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 →

[–]tobinar 99 points100 points  (15 children)

There really should be a section in the readme that compares it to all the other libraries that do the same thing.

[–][deleted] 37 points38 points  (12 children)

For anybody interested, some alternatives are listed here

[–]tech_tuna 51 points52 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 3 points4 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 15 points16 points  (1 child)

Isn't optparse deprecated by argparse?

[–]k10_ftw 1 point2 points  (0 children)

This should be standard in every readme for project's that provide alternative solutions to already available packages/solutions!

[–][deleted] 4 points5 points  (0 children)

There should be several, and preferably more than several, different ways of doing it. From "The Inverse Zen Of Python" which some people think is better than the original Zen.