Dec-02-2019, 05:55 AM
Pages: 1 2
Dec-02-2019, 06:16 AM
you should be able to use argparse: https://docs.python.org/3.8/howto/argparse.html
Dec-02-2019, 06:15 PM
i don't see anything in
argparse
for key=value type arguments. i know C's getopt()
doesn't do them.Dec-02-2019, 07:03 PM
plumbum.cli may allow that. I LOVE this module!
Dec-02-2019, 07:44 PM
i did not see anything about key=value arguments in that module, either.
this is one of the many cases where i am limited to whatever is on the host when the distro is installed. my command scripts need to work without installing anything more. things only i use can have other stuff.
this is one of the many cases where i am limited to whatever is on the host when the distro is installed. my command scripts need to work without installing anything more. things only i use can have other stuff.
Dec-02-2019, 08:36 PM
Skaperen Wrote:i did not see anything about key=value arguments in that module, either.
Here is an example with plumbum.cli, a modified version of an example from the documentation
import logging from plumbum import cli logger = logging.getLogger(__name__) class MyApp(cli.Application): @cli.switch("--log-to-file", str) def log_to_file(self, filename): print('Logging to file', filename) logger.addHandler(logging.FileHandler(filename)) @cli.switch("--verbose", requires = ["--log-to-file"], excludes = ["--terse"]) def verbose(self): logger.setLevel(logging.DEBUG) @cli.switch("--terse", requires = ["--log-to-file"], excludes = ["--verbose"]) def terse(self): logger.setLevel(logging.WARNING) def main(self): logger.info('Execution of' + __file__) if __name__ == "__main__": MyApp.run()
Output:λ python3 paillasse/truccli.py --log-to-file=spam.txt --verbose
Logging to file spam.txt
As you can see, I used --log-to-file=spam.txt
and it worked.Dec-02-2019, 09:10 PM
A example with Click.
# dig.py import click @click.command() @click.option('--hash', type=click.Choice(['MD5', 'SHA1'], case_sensitive=False)) def digest(hash): click.echo(f'Your choice is <{hash}>') if __name__ == '__main__': digest()
Output:E:\div_code
λ python dig.py --hash=MD5
Your choice is <MD5>
E:\div_code
λ python dig.py --hash=SHA1
Your choice is <SHA1>
E:\div_code
λ python dig.py --hash=foo
Usage: dig.py [OPTIONS]
Try "dig.py --help" for help.
Error: Invalid value for "--hash": invalid choice: foo. (choose from MD5, SHA1)
E:\div_code
λ python dig.py --help
Usage: dig.py [OPTIONS]
Options:
--hash [MD5|SHA1]
--help Show this message and exit.
Dec-03-2019, 06:42 AM
i don't mean option assignment. instead of --opt=value leave off the -- and do key=value.
Dec-03-2019, 06:45 AM
Skaperen Wrote:i don't mean option assignment. instead of --opt=value leave off the -- and do key=value.Do you have an example of a command that does this?
Dec-03-2019, 07:23 AM
You can perhaps obtain this effect by tweaking argv's contents like in this example
import argparse import re import sys def parse_args(): parser = argparse.ArgumentParser( description="""\ Program to illustrate the option=value way in argv. """, ) parser.add_argument('--foo', action='store') args = parser.parse_args() print(args) def tweakargv(): """Replace option=value by --option=value in sys.argv""" for i, a in enumerate(sys.argv): if re.match(r"\w+[=]", a): sys.argv[i] = '--' + a if __name__ == '__main__': tweakargv() parse_args()
Output:λ python3 paillasse/tweakargv.py foo=bar
Namespace(foo='bar')
Pages: 1 2