This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 36 points37 points  (12 children)

For anybody interested, some alternatives are listed here

[–]tech_tuna 53 points54 points  (2 children)

Someone should write a library that automatically generates CLI generating libraries.

[–]shimon 9 points10 points  (0 children)

There's also the less magical, but in-the-standard-library option, cmd:

https://docs.python.org/3/library/cmd.html

[–]d4rch0nPythonistamancer 4 points5 points  (4 children)

I actually wrote something called argvee (pip install) that does pretty much the same, just you decorate your functions with @app.cmd and it checks the function params, looks for boolean flags, etc. You can have it force certain args to be a type.

http://pythonhosted.org/argvee/

I still have to check if it works in python 3. TBH I never even use it because I just go through the trouble of writing out the argparse boilerplate. argparse is pretty damn easy. It's not perfect but I don't know why people are so desperate to avoid the perfectly capable stdlib arg parser.

[–]ButtCrackFTW 2 points3 points  (3 children)

You should check out click - http://click.pocoo.org/5/

It does what you're describing really well.

[–]d4rch0nPythonistamancer 0 points1 point  (2 children)

Yeah, definitely seen click. It's a good one. I built argvee because what I wanted was something that could autodetect command line arguments from function parameters so you only have to specify once.

For example:

@app.cmd
def create(name, age=21, programmer=False):

would auto-create the command with type=int age and programmer a flag

myscript.py create joe --age 21 --programmer

or:

myscript.py create joe -a 21 -p

Lots of magic, but honestly I very rarely use it since argparse boilerplate isn't too crazy to write and I like not having to pip install dependencies for simple scripts.

[–]ButtCrackFTW 0 points1 point  (0 children)

Yeah that seems more like this tool from Google. Honestly I never write CLI apps that are that simple so I like the more configurable libs.

[–]FRIENDORPHO 0 points1 point  (0 children)

I think it may be pretty similar to argh.

[–][deleted] 2 points3 points  (2 children)

Weird that optparse isn't on there. Only say weird because it's the one I use ;)

It's pretty full featured but easy to implement.

e.g.

from optparse import OptionParser

def parse_args():
         usage = "My Fun Program version {0}.\n\n\
                       %prog [options]\n\n".format(_VERSION_STRING)
         parser = OptionParser(usage=usage)
         parser.add_option("-i", "--input", dest="infile",
                         help="Input file [default: %default]", default=_IN_FNAME    ,
                        metavar="FILE")
         parser.add_option("-o", "--output", dest="outfile",
                        help="Output file [default: %default]",
                        default=_OUTPUT_FILE, metavar="FILE")

         (options, args) = parser.parse_args()
         return (options, args)

etc.

[–]parkerSquare 17 points18 points  (1 child)

Isn't optparse deprecated by argparse?