Python command line tool - general parsing question

If possible, I would like to use the following structure for the command, however I cannot figure out how to do this in Python:

./somescript.py arg <optional argument> -- "some long argument"

Would it be possible to achieve this as much as possible without unnecessarily dirty code? Or should I just revise the syntax (this is primarily a preference).

Thank!

+3
source share
3 answers

I think optparse can do this.

+3
source

arg optional_argument, Unix, , , . , ...:

import sys

def before_and_after_doubledashes(args=sys.argv):
    where_doubledashes = args.index('--') if '--' in args else len(args)
    return args[:where_doubledashes], args[where_doubledashes+1:]

, args , , ( ) , , , , (, ). :

before, after = before_and_after_doubledashes()

, ( , ..).

+1

- :   #/USR///

, python.

args getopt optparser

chmod , .

0
source

Source: https://habr.com/ru/post/1750397/


All Articles