Python Forum
key=value style command argument parser
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
key=value style command argument parser
#1
for a command that will use command line arguments like key=value for all arguments, is there an available parser?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
you should be able to use argparse: https://docs.python.org/3.8/howto/argparse.html
Reply
#3
i don't see anything in argparse for key=value type arguments. i know C's getopt() doesn't do them.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
plumbum.cli may allow that. I LOVE this module!
Reply
#5
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
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.
Reply
#7
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.
Reply
#8
i don't mean option assignment. instead of --opt=value leave off the -- and do key=value.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
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?
Reply
#10
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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  my own command line option parser Skaperen 0 1,645 Mar-27-2020, 04:14 AM
Last Post: Skaperen
  argument for the python command Skaperen 0 2,629 Dec-05-2019, 11:30 PM
Last Post: Skaperen
  parser for LVM head Skaperen 0 1,421 Nov-02-2019, 04:04 AM
Last Post: Skaperen
  cli arguments parser Skaperen 0 2,601 Mar-21-2018, 01:41 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020