argparse optional dependency for optional argument - python

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)

Related

How do I create new command line flags in Ubuntu

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.

Python CLI Framework and Argument Parsing with Tab Completion

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.

How to construct argparse parameters for different actions

How would you construct a Python argparse parser to support different parameters organized by a top-level action, as well as have argparse's default help functionality distinguish between the different action/option groups?
e.g.
$ myprog.py list --verbose
Listing records:
...
$ myprog.py run --iterations=10
Running
...
$ myprog.py help
usage: myprog.py [action]
positional arguments:
action {list,run,help}
list Lists records.
run Process records.
help Show action help.
$ myprog.py help run
usage: myprog.py run [--iterations=N] [--verbose] [--skip]
optional arguments:
--iterations N Process N records.
--verbose Show extra messaging.
--skip Skip previously seen IDs.
I thought this might be supported by the parents feature, but this seems more designed to grouping option groups than separate argument actions.
here's an example I made of using parsers and subparsers with argparse to achieve something close to what you're looking for, and that's a code to access SO by CLI ;-)
I'd also advise you to have a look at docopt
You could parse the first item, make your choice, then have different argument parsers for each item. If you want to share some commands, then you can use the parents feature to share some arguments between your various commands.
parents is a convenient way of adding arguments to one or more parsers. It does not affect the parsing or the help.
add_argument_group is a way of grouping arguments in the help. It may be what you need. It does not, though, affect how the arguments are parsed. The default groupings are 'positional' and 'optional' (meaning 'uses a flag').
add_subparsers is a way of both grouping arguments and their parsing. With it you create new parsers that are invoked with simple commands. Each subparser has its own set of arguments (and own help display)
If you want to organize the help in an entirely different way, consider writing your own usage and description, and suppressing the default help lines with help=argparse.SUPPRESS.
IPython is an advanced user of argparse. It detects help in sys.argv before calling argparse, and thus bypasses the default help.

vim add automatical sphinx comment under function and class definition

I want to automatically add sphinx comment under head functions and classes.
When I press Enter after head function or class, comment could be implemented like this:
def func(a): #<Enter>
"""
Args:
a (type): The name to use.
Returns:
type. The return
"""
Is it possible to configure .vimrc (.vimrc.local)? Do you know command for this? Or may be plugin?
Though you can do this with the built-in (insert-mode) mappings, you'll soon want to do more advanced insertions.
snippets are like the built-in :abbreviate on steroids, usually with parameter insertions, mirroring, and multiple stops inside them. One of the first, very famous (and still widely used) Vim plugins is snipMate (inspired by the TextMate editor); unfortunately, it's not maintained any more; though there is a fork. A modern alternative (that requires Python though) is UltiSnips. There are more, see this list on the Vim Tips Wiki.
There are two things to evaluate: First, the features of the snippet engine itself, and second, the quality and breadth of snippets provided by the author or others.

What does dry-run do in optparse Python?

What's the functionality of the dry-run option in the optparse module of Python?
Dry run is a generic expression in many fields, including computing, meaning that a certain operation should be performed or simulated limiting its dangerous effects.
It is up to you to associate that option to something meaningful in your code. For example: if your script normally removes files from the hard drive, the --dry-run option should only print out a list of the files that would have been deleted if the script would have been ran without the --dry-run option.
--dry-run is just a conventional name for that option, but you could implement the same functionality with any other name (e.g --simulate-only or --dont-screw-up).
--dry-run has no special meaning for the optparse module.
It is just an example used in the documentation of the module.

Categories