Python Forum
key=value style command argument parser - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: key=value style command argument parser (/thread-22894.html)

Pages: 1 2


key=value style command argument parser - Skaperen - Dec-02-2019

for a command that will use command line arguments like key=value for all arguments, is there an available parser?


RE: key=value style command argument parser - Larz60+ - Dec-02-2019

you should be able to use argparse: https://docs.python.org/3.8/howto/argparse.html


RE: key=value style command argument parser - Skaperen - Dec-02-2019

i don't see anything in argparse for key=value type arguments. i know C's getopt() doesn't do them.


RE: key=value style command argument parser - Gribouillis - Dec-02-2019

plumbum.cli may allow that. I LOVE this module!


RE: key=value style command argument parser - Skaperen - Dec-02-2019

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.


RE: key=value style command argument parser - Gribouillis - Dec-02-2019

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.


RE: key=value style command argument parser - snippsat - Dec-02-2019

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.



RE: key=value style command argument parser - Skaperen - Dec-03-2019

i don't mean option assignment. instead of --opt=value leave off the -- and do key=value.


RE: key=value style command argument parser - Gribouillis - Dec-03-2019

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?


RE: key=value style command argument parser - Gribouillis - Dec-03-2019

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')