开发者

Arbitrary command line arguments in python

开发者 https://www.devze.com 2023-01-09 12:30 出处:网络
I want to allow arbitrary command line ar开发者_C百科guments.If the user provides me with a command line that looks like this

I want to allow arbitrary command line ar开发者_C百科guments. If the user provides me with a command line that looks like this

myscript.py --a valueofa --b valueofb posarg1 posarg2

I know that a was passed with valueofa, b passed with valueofb and that I have these last two positional arguments.

I've always used optparse, for which you specify exactly which arguments to look for. But I want the user to be able to define arbitrary "macros" from the command line. Surely there's a python module that does it more elegantly than anything I'd write. What is?


arbitrary_args.py:

#!/usr/bin/env python3

import sys


def parse_args_any(args):
    pos = []
    named = {}
    key = None
    for arg in args:
        if key:
            if arg.startswith('--'):
                named[key] = True
                key = arg[2:]
            else:
                named[key] = arg
                key = None
        elif arg.startswith('--'):
            key = arg[2:]
        else:
            pos.append(arg)
    if key:
        named[key] = True
    return (pos, named)


def main(argv):
    print(parse_args_any(argv))


if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))

$./arbitrary_args.py cmd posarg1 posarg2 --foo --bar baz posarg3 --quux:

(['cmd', 'posarg1', 'posarg2', 'posarg3'], {'foo': True, 'bar': 'baz', 'quux': True})

argparse_arbitrary.py:

#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-D', action='append', )
D = {L[0]:L[1] for L in [s.split('=') for s in parser.parse_args().D]}
print(D)

$./argparse_arbitrary.py -Ddrink=coffee -Dsnack=peanut

{'snack': 'peanut', 'drink': 'coffee'}


Sadly, you can't. If you have to support this, you'll need to write your own option parser =(.


Will argparse do what you want? It was recently added to the standard library. Specifically, you might want to look at this section of the documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消