def create_argparser(meta_args): parser = ArgumentParser(description='ArgumentParser created from meta-CLI') kwargs = {k: v for k, v in vars(meta_args).items() if v != EMPTY} argname = kwargs.pop('argname') argform = kwargs.pop('argform') if 'action' in kwargs: kwargs['action'] = kwargs['action'].value adder = getattr(parser, 'add_%s' % argform.name.lower()) print('# %s %r' % (adder.__name__, kwargs)) adder(argname, **kwargs) return parser
def _parse(self, arg_type, *args, cli_args=None, **kwargs): ap = AP() add_func = getattr(ap, 'add_' + arg_type) add_func(*args, **kwargs) if cli_args is not None: return ap.parse_args(cli_args.split())
def get_meta_args(): parser = ArgumentParser(description=CALLER_DOC, argument_default=EMPTY) parser.add_positional('argform', type=ArgForm) parser.add_positional('argname') parser.add_optional('nargs', type=nargs_type) parser.add_optional('default', type='literal') parser.add_optional('type', type=lambda s: getattr(builtins, s, s)) parser.add_optional('arghelp', dest='help', help='the "help" param') parser.add_optional('metavar') parser.add_optional('dest') parser.add_optional('action', type=Action) parser.add_optional('const', type='literal') parser.add_flag('required') parser.add_flag('strict-default') args, rest = parser.parse_known_args() if rest and rest[0] == '--': rest = rest[1:] return args, rest
""" Print data stored in a pickle file (optionally compressed). This simple script demonstrating `pickled_data` arg type. """ from apegears import ArgumentParser, CALLER_DOC if __name__ == '__main__': parser = ArgumentParser(description=CALLER_DOC) parser.add_positional('data', type='pickled_data') args = parser.parse_args() print(args.data)
""" Print a sequence of numbers. This is a simple ``seq`` script, demonstrating range arg type. """ from apegears import ArgumentParser, CALLER_DOC if __name__ == '__main__': parser = ArgumentParser(description=CALLER_DOC) parser.add_positional('seq', type=range) args = parser.parse_args() for i in args.seq: print(i)
""" cat - concatenate files and print on the standard output. This is a simple ``cat`` script, demonstrating fileinput arg type. """ from apegears import ArgumentParser, fileinput, CALLER_DOC if __name__ == '__main__': parser = ArgumentParser(description=CALLER_DOC) parser.add_positional(type=fileinput(decompress=True), nargs='*') args = parser.parse_args() for line in args.infiles: print(line, end='')
""" Copy a file. This is a simple ``copy`` script, demonstrating FileTypes. """ from apegears import ArgumentParser, FileType, CALLER_DOC if __name__ == '__main__': parser = ArgumentParser(description=CALLER_DOC) parser.add_positional('src', type=FileType('rb')) parser.add_positional('dest', type=FileType('wb')) args = parser.parse_args() # read: contents = args.src.read() # write: # NOTE: dest file is only created when writing to it (which is the point of this example): args.dest.write(contents)