3 ms·
>> Flags with single character shortcuts can be easily combined—symbex -in fetch_data is short for symbex --imports --no-file fetch_data for example. I pretty
by wedn3sday 3y ago
>> Flags with single character shortcuts can be easily combined—symbex -in fetch_data is short for symbex --imports --no-file fetch_data for example.
I pretty much use argparse for making all my CLI tools, but I dont know of an easy way of doing this single character flag thing. Is it possible/easy with argparse?
- jmholla 3y ago`argparse` does it by default: >>> import argparse >>> p = argparse.ArgumentParser() >>> p.add_argument("--foo", "-f", action="store_true") >>> p.add_argument("--bar", "-b") >>> p.parse_args(["-fb", "baz"]) Namespace(foo=True, bar='baz')
- m463 3y agoI use argparse too, and it's one of the best python libraries (and my most-used) you can do short (one character) or long arguments with argparse directly: parser = argparse.ArgumentParser(argument_default=None) parser.add_argument('-d', '--debug', action='store_true', help='debug flag') I also do lots of other things, like long help with no args like this: if len(sys.argv) == 1: parser.print_help(sys.stderr) sys.exit(1)