argparse

How to get advanced usage of getop() in php like python argparse

扶醉桌前 提交于 2019-12-08 03:39:46
问题 I commonly use argparse module in Python to detect options/parameters, print usage, etc. I am trying to get the same results in PHP from native code or using some lightweight library/framework without writing a lot of wrapping lines. From my research, I've just found getop()'s native PHP implementation, that is quite like getop()'s C implementation and very limited as a framework for certain uses (exclusive options, prefix options, parameter conflict handler, choices, metavars, etc). I would

Options with Options with Python argparse?

青春壹個敷衍的年華 提交于 2019-12-08 01:41:05
问题 I'm writing a script in Python, and using argparse to parse my arguments. The script is supposed to compare two different "aligners" from a pool of available aligners, and each aligner has some configuration options. I want to be able to call my script with something like: ./script.py --aligner aligner1 --param 12 --aligner aligner2 --param 30 --other_param 28 I want to get out of this some sort of structure where the first --param option "belongs" to the first --aligner option, and the

argparse change definition of argument

落爺英雄遲暮 提交于 2019-12-08 00:33:01
问题 I set up my argument parser as follows: parser=argparse.ArgumentParser() parser.add_argument('--point',help='enter a point (e.g. 2,3,4)') parser.parse_args('--point=-2,5,6'.split()) #works parser.parse_args('--point -2,5,6'.split()) #doesn't work :( Is there any way to tell argparse that strings which match the regular expression r"-\d+.*" are not options but an argument of an option? Also note that I could do something like this: parser.add_argument('--point',nargs='*') parser.parse_args('-

Sub-classing the argparse Argument Parser

↘锁芯ラ 提交于 2019-12-07 21:07:48
问题 I am trying to write a parser class derived from the Python argparse ArgumentParser class. The outlines of the following code work fine on the command line but generate an error I am struggling to understand in the context of my module. The code (stripped down a little to remove the unimportant stuff) is as follows: class SansParser(argparse.ArgumentParser): """Argument parser for preparing a SansModel fit or calculation """ def __init__(self): """Initialisation method for the parser class"""

Parser in python3 does not take delimiter values from commandline via argparse

雨燕双飞 提交于 2019-12-07 19:29:55
问题 I have written a simple script as an advanced tool for my awk / sed requirements. In the script I compare two files on basis of values from one column of the query file and then extract whole entries from the master file. The script allows you to enter the values for columns and delimiters for each file. The problem is that the 'delimiter' options are not recognized by script when given from command line. Here is my code (partial): ##- - - - - - - -- - - - - - Arguments - - - - - - - - - - -

argparse: Associate arguments with another argument

允我心安 提交于 2019-12-07 18:19:44
问题 With argparse, it's possible to repeat an argument and collect all the values into a list: parser = ArgumentParser() parser.add_argument('-o', '--output', action='append') args = parser.parse_args(['-o', 'output1', '-o', 'output2']) print(vars(args)) # {'output': ['output1', 'output2']} I'm looking for a way to associate flags with each of these arguments, so that it's possible to do this: args = parser.parse_args(['-o', 'output1', '--format', 'text', '-o', 'output2', '--format', 'csv']) And

Python argparse: How to change `--add` into `add` while still being an optional argument?

跟風遠走 提交于 2019-12-07 17:29:48
问题 I want this functionality: $ python program.py add Peter 'Peter' was added to the list of names. I can achieve this with --add instead of add like this: import argparse parser = argparse.ArgumentParser() parser.add_argument("--add", help="Add a new name to the list of names", action="store") args = parser.parse_args() if args.add: print "'%s' was added to the list of names." % args.add else: print "Just executing the program baby." Such that: $ python program.py --add Peter 'Peter' was added

Conditional argparse with choice option

蓝咒 提交于 2019-12-07 14:42:39
问题 Below are three arguments I am writing in a module. parser.add_argument('--Type',type=str,choices=['a','b','c'],help='Options include: a,b,c.',required=True) parser.add_argument('--Input',default=False,help='Generate input files',required=False) parser.add_argument('--Directory',default=False,help='Secondary directory',required='--Input' in sys.argv) The --Type is possible with three options: a,b,c. Currently, I have it set up so that, if --Directory is true, it requires --Input to be true.

Argparse custom help from text file

风格不统一 提交于 2019-12-07 06:47:41
问题 I want to use the argparse library because of its flexibility, but I am having trouble disabling the default help dialogue to show a custom one from a text file. All I want to do is display the text from the text file when the "-h" or "--help" option is passed. Here is an example of how I am trying this: parser = argparse.ArgumentParser(add_help=False) parser.add_argument("file", type=str, nargs='+') parser.add_argument("-xmin", type=float) parser.add_argument("-xmax", type=float) parser.add

Using python's argparse in multiple scripts backed by multiple custom modules

↘锁芯ラ 提交于 2019-12-07 04:11:50
问题 I'm building out a set of scripts and modules for managing our infrastructure. To keep things organized I'd like to consolidate as much effort as possible and minimize boiler plate for newer scripts. In particular the issue here is to consolidate the ArgumentParser module. An example structure is to have scripts and libraries organized something like this: |-- bin |-- script1 |-- script2 |-- lib |-- logger |-- lib1 |-- lib2 In this scenario script1 may only make use of logger and lib1 , while