Python Forum

Full Version: key=value style command argument parser
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
for a command that will use command line arguments like key=value for all arguments, is there an available parser?
you should be able to use argparse: https://docs.python.org/3.8/howto/argparse.html
i don't see anything in argparse for key=value type arguments. i know C's getopt() doesn't do them.
plumbum.cli may allow that. I LOVE this module!
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.
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.
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.
i don't mean option assignment. instead of --opt=value leave off the -- and do key=value.
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?
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