Bottom Page

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?
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#2
you should be able to use argparse: https://docs.python.org/3.8/howto/argparse.html
Quote
#3
i don't see anything in argparse for key=value type arguments. i know C's getopt() doesn't do them.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#4
plumbum.cli may allow that. I LOVE this module!
Larz60+ likes this post
Quote
#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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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.
Quote
#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.
Quote
#8
i don't mean option assignment. instead of --opt=value leave off the -- and do key=value.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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?
Quote
#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')
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  argument for the python command Skaperen 0 226 Dec-05-2019, 11:30 PM
Last Post: Skaperen
  parser for LVM head Skaperen 0 106 Nov-02-2019, 04:04 AM
Last Post: Skaperen
  Finetuning GPT-2 twice for particular style of writing on a particular topic regstuff 0 105 Oct-25-2019, 04:39 AM
Last Post: regstuff
  is there an options parser that can handle + and ++ Skaperen 1 188 Jul-20-2019, 07:42 PM
Last Post: metulburr
  cli arguments parser Skaperen 0 1,170 Mar-21-2018, 01:41 AM
Last Post: Skaperen

Forum Jump:


Users browsing this thread: 1 Guest(s)