A lightweight, dynamic argument parsing library for Python programs with klugy support for typing variables.
I made this for a teaching machine project I'm working on (I needed a custom argument parser for reasons),
and I'm always too impatient to use argparse.
Find this tool on PyPI: pip install arglite
Check this out:
import arglite
def main():
# Can include explicit requirement
print(arglite.parser.required.a)
# Can be an implicit requirement
print(arglite.parser.b)
# Can also be purely optional
print(arglite.parser.optional.c)
print(arglite.parser.optional.d)
if __name__ == "__main__":
main()Run using python main.py -a Yo --b that is -c.
For the more intrepid among us, this also works:
from arglite import parser as cliarg
def main():
# Can include explicit requirement
print(cliarg.required.a)
# Can be an implicit requirement
print(cliarg.b)
# Can also be purely optional
print(cliarg.optional.c)
print(cliarg.optional.d)
if __name__ == "__main__":
main()Help now appears when no variables are provided or when requested by use of -h (--h) or -help (--help).
It looks like this:
arglite
argparse is a CLI argument parser for the impatient
Hi! You're seeing this message because you used a help flag or
because there were no variables specified on the command line as
flags!
Usage
- Provide arbitary flags to a program at runtime
- Interpret flags with the argparse.parser object
CLI flags
┏━━━━━━━━┳━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━┓
┃Variable name┃Variable value┃Variable type┃Variable required┃
┡━━━━━━━━╇━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━┩
│ a │ None │ NoneType │ 🗸 │
│ b │ None │ NoneType │ 🗸 │
│ c │ None │ NoneType │ ✗ │
│ d │ None │ NoneType │ ✗ │
└───────────────┴──────────────┴──────────────┴─────────────────┘
When errors are present (i.e. flags are provided which aren't used in the code or flags used aren't provided), you'll see errors:
✗ ERROR: A value was provided for A, but the program doesn't call for it
✗ ERROR: A value was expected for a, but not was provided as a flag
✗ ERROR: A value was expected for b, but not was provided as a flag
- Flags with no value are automatically converted to
Trueboolean - The module uses
ast.literal_eval, so"{'a':'b'}"will convert to adict(all quotes required)