I'm building Python Twisted applications and would like to use the argparse module to parse command line options, instead of the Twisted usage.Options, which seems kind of old school.
I'd like to run the app with twistd, and am concerned about argparse interfering with twistd's command line argument parsing. I am seeking pointers, suggestions or opinions on what I'm trying to do.
Your twistd plugins are required to parse command-line options by providing a class with a parseOptions method that takes an argv-style list. Beyond that, what you do is up to you. If you supply a class with that method that calls into argparse, great.
Related
I have files saved into a "file1.ors" format. In this case, they are similar to dictionary entries. I have been tasked with now converting them to other formats, such as HTML, Bibtext, or JSON. However, my supervisor wants me to use python from the command line, and he wants the choice to be decided with new command-line flags. For example,
$ orsconvert.py --json file1.ors
which would take file1.ors and convert it into a JSON format. The issue is that I cannot even find anything on how to create/define my own command line flags that I can use with the program. Any suggestions on how to do this?
Use library argparser, this lib comes pre-installed with Python and very userful for creating command line options. Also, will provide a leverage to use -h command to get all the necessay help (check the first example in the doc).
As an alternative to the built-in argparse library — which in my experience is rather weird to use, especially when you want to implement a complex command line interface —, you might also want to look into Typer which uses Python's new type annotations feature and is somewhat easier to grasp.
I am currently building an extension to an existing library that uses argparse.ArgumentParser() to ingest cli arguments, and have added my own argparse.ArgumentParser() using parse_known_args() to parse out the arguments I want to inject before passing the remaining arguments on to the existing library's implementation. I am doing this instead of adding the arguments to the underlying library so that if the underlying library changes, everything should still work.
However, when I pass in the --help flag my implementation of argparse.ArgumentParser() grabs that flag, prints out help for my injected arguments then breaks execution so it never gets to the help message for the underlying library.
I am having trouble figuring out if there's a way to combine the help directives for both argparse.ArgumentParser() implementations (if it's even possible), or how to ignore the help flag in my implementation. My added arguments are for debug only and are not vital to display to the end user of my extended library, so if there is a way to ignore the help flag in my implementation, that would be OK, while not ideal.
I think this may be an answer to your question. add_help=False, but added to subcommands and subparsers. Python argparse - disable help for subcommands?
You need to pass parents parameter to the subsequent parsers. I've just posted an answer to a similar question here:
Argparse: is it possible to combine help texts from multiple parsers?
I am looking for a good module that is already out there for parsing arguments on custom CLI interfaces. If you can imagine there is a CLI that gives you a prompt:
framework> commands go here....
It starts to get tedious when you add in arguments, example:
framework> command <argument1> <argument2> ...
I'd like to know if there is any sort of module that will not only make it easy to create these CLI interfaces, but one that has tab completion for each argument in a command chain. I'm tired of reinventing the wheel each time I create something like this!
framework> comma<tab>
framework> command arg<tab>
framework> command argument parame<tab>
framework> command argument parameter
You get the idea. If there is no module already out there for this sort of thing, any suggestions on an elegant solution to creating something like this in a modularized fashion, it would be much appreciated.
Please note that I do not want to parse arguments passed in through the parent process command shell (such as bash), I want to do the processing from within an infinite while loop within the script itself. For example:
while (True):
cmd = raw_input("framework> ")
framework.process_command(cmd)
Take a look at Click, "a Python package for creating beautiful command line interfaces in a composable way with as little code as necessary": http://click.pocoo.org/5/
in python you have a build in module called argparse to manage CLI parameters application, now take a look to this module argparse extension for tab completion https://pypi.python.org/pypi/argcomplete/0.8.4
Looks like the builtin cmd module offers what you're looking for. It supports tab completion for commands and arguments. Here's a tutorial from PyMOTW. Another example here.
If you need some more customizations, there's also cmd2 which extends the builtin cmd.
I need to get to work something like this:
./foo.py [-b option [-a]]
with argparse. I thought about using sub_parsers, but I don't know how to make it work.
The straightforward way is to simply add rules in the codes that gets your options to throw an error if -a is there and not b (and document it in the user help). You can also override the usage string using the usage keyword: http://docs.python.org/dev/library/argparse.html#usage
sub_parsers are nice, but I don't think they fit your needs. Their usecase is more like putting a lot of features in a single executable (think svn add, svn ci, svn co, etc)
I have an argument that is an internal debug flag and shouldn't be run by users who don't know what they are doing.
I realize that hiding it is in essence security by obscurity, but I'm not concerned about malice so much as incompetence.
Optparse had a SUPPRESS_HELP option, is there and argparse equivalent?
parser.add_argument('--secret', help=argparse.SUPPRESS)
From here: http://argparse.googlecode.com/svn/trunk/doc/argparse-vs-optparse.html
First Google result for argparse suppress_help, by the way.