Esempio n. 1
0
    def create_ui(self):
        """
        create a dialog with the options from the parser
        and an ok and cancel button
        """
        self.addDescription()
        self.actionLookupTable = { 
                    type(argparse._HelpAction(None)) : self.makeHelpActionEntry,
                    type(argparse._VersionAction(None)) : self.makeHelpActionEntry,                   
                    type(argparse._StoreAction(None,None,None)): self.makeStoreConstEntry,
                    type(argparse._StoreConstAction(None,None,None)): self.makeStoreConstEntry,
                    type(argparse._StoreTrueAction(None, None)): self.makeStoreConstEntry,
                    type(argparse._StoreFalseAction(None, None)): self.makeStoreConstEntry,
                    type(argparse._StoreAction(None, None)) : self.makeStoreActionEntry,
                    type(argparse._CountAction(None, None)) : self.makeCountActionEntry,
                    type(argparse._AppendAction(None,None)) : self.makeAppendActionEntry,
                    type(argparse._AppendConstAction(None, None, None)) : self.makeCountActionEntry
                    }
                    
        for a in self.parser._get_optional_actions():
            try:
                self.actionLookupTable[type(a)](a, optional=True)
                #print ("Introspected type: {0}\n".format(a))
            except KeyError:
                print ("Unsupported type: {0}\n".format(a))

        for a in self.parser._get_positional_actions():
            try:
                self.actionLookupTable[type(a)](a, optional=True)
                #print ("Introspected type: {0}\n".format(a))
            except KeyError:
                print ("Unsupported type: {0}\n".format(a))

            
        self.addEpilog()
Esempio n. 2
0
 def add_usage(self, usage, actions, groups, prefix=None):
     """
     Hack add_usage to add fake "-- command [arguments]" to usage
     """
     actions.append(
         argparse._StoreAction(option_strings=[],
                               dest="-- command [arguments]"))
     return super(CustomFormatter, self).add_usage(usage, actions, groups,
                                                   prefix)
Esempio n. 3
0
 def _parse_optional(self, arg_string):
     option_tuple = super(KwargsParser, self)._parse_optional(arg_string)
     long_option = all(char in self.prefix_chars for char in arg_string[:2])
     if long_option and option_tuple == (None, arg_string, None):
         action, option_string, explicit_arg = option_tuple
         action = argparse._StoreAction(
             **self._get_optional_kwargs(arg_string))
         option_tuple = (action, option_string, explicit_arg)
     return option_tuple
Esempio n. 4
0
 def add_usage(self, usage, actions, groups, prefix=None):
     """
     Add '-- command [arguments]' to usage.
     """
     actions.append(
         argparse._StoreAction(option_strings=[],
                               dest="-- command [arguments]"))
     return super(ArgsFormatter, self).add_usage(usage, actions, groups,
                                                 prefix)
Esempio n. 5
0
    def add_usage(self, usage, actions, groups, prefix=None):
        """ Hack add_usage to add fake "-- command [arguments]" to the usage

        """
        actions.append(argparse._StoreAction(
            option_strings=[],
            dest="-- command [arguments]"
        ))
        return super(CustomFormatter, self).add_usage(
            usage, actions, groups, prefix
        )
Esempio n. 6
0
def generate_base_doc(app, hamm_help):
    myactions = []
    cmds = sorted(app.subCmds)
    for cmd in cmds:
        myactions.append(
            argparse._StoreAction(option_strings=[],
                                  dest=str(cmd),
                                  nargs=None,
                                  const=None,
                                  default=None,
                                  type=str,
                                  choices=None,
                                  required=False,
                                  help=str(app.subCmds[cmd].__doc__),
                                  metavar=None))
    return myactions
Esempio n. 7
0
def generate_base_doc(app, hamm_help):
    myactions=[]
    cmds= sorted(app.subCmds)
    for cmd in cmds:
        myactions.append(argparse._StoreAction(
            option_strings=[],
            dest=str(cmd),
            nargs=None,
            const=None,
            default=None,
            type=str,
            choices=None,
            required=False,
            help=str(app.subCmds[cmd].__doc__),
            metavar=None))
    return myactions
Esempio n. 8
0
    def __init__(self, *args, **kwargs):
        self.priority = int(kwargs.pop("priority", self.default_priority))
        self.config_type = kwargs.pop("config_type", None)
        if self.config_type is None or not isinstance(self.config_type, str):
            raise ValueError("config_type must specified, and be a string")

        if kwargs.pop("get_default", False):
            kwargs["default"] = DelayedValue(
                partial(self.store_default, self.config_type,
                        option_string=kwargs.get('option_strings', [None])[0]),
                self.priority)

        self.store_name = kwargs.pop("store_name", False)
        self.writable = kwargs.pop("writable", None)
        self.target = argparse._StoreAction(*args, **kwargs)

        super(StoreConfigObject, self).__init__(*args, **kwargs)
Esempio n. 9
0
    def __init__(self, *args, **kwargs):
        self.priority = int(kwargs.pop("priority", self.default_priority))
        self.config_type = kwargs.pop("config_type", None)
        if self.config_type is None or not isinstance(self.config_type, str):
            raise ValueError("config_type must specified, and be a string")

        if kwargs.pop("get_default", False):
            kwargs["default"] = DelayedValue(
                partial(self.store_default, self.config_type,
                        option_string=kwargs.get('option_strings', [None])[0]),
                self.priority)

        self.store_name = kwargs.pop("store_name", False)
        self.writable = kwargs.pop("writable", None)
        self.target = argparse._StoreAction(*args, **kwargs)

        super(StoreConfigObject, self).__init__(*args, **kwargs)
Esempio n. 10
0
def store_env_override(option_strings,
                       dest,
                       envvar,
                       nargs=None,
                       default=None,
                       type=None,
                       choices=None,
                       description=None,
                       addendum=None,
                       help=None,
                       metavar=None):
    """Construct an argparse action which stores the value of a command
    line option to override a corresponding value in the process
    environment.

    If the environment variable is not empty, then no override is
    required. If the environment variable is empty, and no default is
    provided, then the "option" is required.

    In the case of a default value which is a *transformation* of the
    single environment variable, this default may be provided as a
    callable, (*e.g.* as a lambda function).

    Rather than have to fully explain the relationship of this
    environment-backed option, help text may be generated from a
    provided description. (And an addendum may be optionally appended to
    the end of the generated text.)

    To aide in the differentiation of whether the resulting value
    originated from the command line or the process environment, the
    environment-derived default (and its optional transformation) are
    wrapped in the ``str`` subclass: ``EnvDefault``.

    """
    if envvar == '':
        raise ValueError("unsupported environment variable name", envvar)

    envvalue = os.getenv(envvar)

    if callable(default):
        default_value = EnvDefault(default(envvalue))
    elif envvalue:
        default_value = EnvDefault(envvalue)
    else:
        default_value = default

    if description and help:
        raise ValueError(
            "only specify help to override its optional generation from "
            "description -- not both")
    elif description:
        if default_value:
            help = '{} (default {} envvar {}: {})'.format(
                description,
                'provided by' if default is None else 'derived from',
                envvar,
                default_value,
            )
            if addendum:
                help += f' {addendum}'
        else:
            help = (f'{description} (required because '
                    f'envvar {envvar} is empty)')
    elif addendum:
        raise ValueError(
            "addendum intended for use in conjunction with description")

    return _StoreAction(
        option_strings=option_strings,
        dest=dest,
        nargs=nargs,
        const=None,
        default=default_value,
        type=type,
        choices=choices,
        required=(not default_value),
        help=help,
        metavar=metavar,
    )
Esempio n. 11
0
 def __call__(self, *args, **kwargs):
     if 'type' in kwargs:
         raise ValueError('ActionOperators does not allow type given to add_argument.')
     kwargs['type'] = self._type
     return _StoreAction(**kwargs)