argparse

Namespace, argparse, and usage

匆匆过客 提交于 2019-12-05 09:20:23
问题 This is really a few questions: Is there a reason argparse uses a namespace instead of a dictionary? Assuming I have a class with __init__(self, init_method, *args) . The init_method parameter tells the init_function which way I want to initialize the class, while arg parameter gives all the arguments neccesary for the init. The arguments may be different for different methods. Should I use a dictionary, or a namespace? Assuming that I use a namespace, how do I pass the namespace to __init__(

How to localize Python's argparse module, without patching it?

匆匆过客 提交于 2019-12-05 08:18:52
A localized command line application looks strange when some part of the messages are in the user language and some other parts, in English. I don't know if I messed up anything when I installed Python 3 from source, it seems there are no *.mo files, so argparse (among the whole) is not localization aware. The API does not seems to offer a way to localize, neither. Or did I missed it? I could patch argparse.py , but I won't, as I want it to be portable, and I'm not OK with suggesting users to patch their Python installation. Question in fewer words: how to localize argpase without patching

Can two Python argparse objects be combined?

偶尔善良 提交于 2019-12-05 06:53:22
I have an object A which contains parserA - an argparse.ArgumentParser object There is also object B which contains parserB - another argparse.ArgumentParser Object A contains an instance of object B, however object B's arguments now need to be parsed by the parser in object A (since A is the one being called from the command line with the arguments, not B) Is there a way to write in Python object A: parserA += B.parserB? argparse was developed around objects. Other than a few constants and utility functions it is all class definitions. The documentation focuses on use rather than that class

Python. Argparser. Removing not-needed arguments

不打扰是莪最后的温柔 提交于 2019-12-05 06:43:13
I am parsing some command-line arguments, and most of them need to be passed to a method, but not all. parser = argparse.ArgumentParser() parser.add_argument("-d", "--dir", help = "Directory name", type = str, default = "backups") parser.add_argument("-n", "--dbname", help = "Name of the database", type = str, default = "dmitrii") parser.add_argument("-p", "--password", help = "Database password", type = str, default = "1123581321") parser.add_argument("-u", "--user", help = "Database username", type = str, default = "Dmitriy") parser.add_argument("-a", "--archive", help = "Archive backup",

call_command argument is required

≡放荡痞女 提交于 2019-12-05 05:38:51
I'm trying to use Django's call_command in a manner very similar to this question without an answer . The way I'm calling it is: args = [] kwargs = { 'solr_url': 'http://127.0.0.1:8983/solr/collection1', 'type': 'opinions', 'update': True, 'everything': True, 'do_commit': True, 'traceback': True, } call_command('cl_update_index', **kwargs) In theory, that should work, according to the docs . But it doesn't work, it just doesn't. Here's the add_arguments method for my Command class: def add_arguments(self, parser): parser.add_argument( '--type', type=valid_obj_type, required=True, help='Because

Ruby optparse Limitations

匆匆过客 提交于 2019-12-05 05:37:48
I currently script in Python but I wish to try Ruby for several reasons. I've looked at a lot of sample code and read a lot of documentation over the last week. One point of concern I have is the lack of a proper command line argument parsing libraries in Ruby. Ruby experts, don't get mad at me — maybe I don't know. That's why I am here. In Python, I was used to using argparse which in my opinion is simply perfect (maybe for my needs). Unfortunately though, OptionParser doesn't allow for the flexibility and features that argparse does. I am specifically looking at the following constraints for

Python argparse regex expression

冷暖自知 提交于 2019-12-05 03:52:33
Is it possible to use a regex expression for parsing an argument? For example, I want to accept an argument if only it is a 32 length hex (i.e. matches /[a-f0-9A-F]{32}/ ) I tried p.add_argument('hex', type=str, nargs="[a-f0-9A-F]{32}") without success This is what the type kwarg is used for: it can take any callable that takes a single string argument and returns the converted value. import argparse import re from uuid import uuid4 def my_regex_type(arg_value, pat=re.compile(r"^[a-f0-9A-F]{32}$")): if not pat.match(arg_value): raise argparse.ArgumentTypeError return arg_value parser =

Python argparse parse_args into global namespace (or a reason this is a bad idea)

一曲冷凌霜 提交于 2019-12-05 02:22:14
I've mostly used argparse for making command-line scripts in python, and the idiom I generally use is that I assign the arguments as attributes of an object, then parse them individually to a variable that matches their attribute name. This seems a little repetitive. Is there a way to assign them all into the global namespace and cut out the assignment step; or as is often the case when some python behavior seems counter-intuitive to me, can some wise, python expert point out that there a good reason I should not do this or want to do this? What I have now is this: if __name__ == "__main__":

Can Python's argparse permute argument order like gnu getopt?

风流意气都作罢 提交于 2019-12-05 02:03:43
GNU getopt, and command line tools that use it, allow options and arguments to be interleaved, known as permuting options (see http://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html#Using-Getopt ). Perl's Getopt::Long module also supports this (with qw(:config gnu_getopt)). argparse seems to not support (or even mention) permuting options. There are many SO questions related to arg/opt order, but none seem answer this question: Can argparse be made to permute argument order like getopt? The use case is a prototypical command line signature like GNU sort: sort [opts] [files] in

tf.app.flags.DEFINE_string()和tf.app.flags.FLAGS

时光总嘲笑我的痴心妄想 提交于 2019-12-05 01:19:27
在看SSD的TensorFlow源码(链接 点击打开链接 )时遇到了这tf.app.flags.DEFINE_string()函数和tf.app.flags.FLAGS变量,于是翻阅TensorFlow官网API结果竟然是。。。 真是相当于白说,意思还是让我自己看源码。。。。。。。 于是上网找,还是找到了Stack Overflow里的一个解答 点击打开链接 答案说其实这个,很多时候看TensorFlow的API文档不如看TensorFlow的源码更容易(真的么。。。),所以答主经常用pycharm打开TensorFlow源码的工程,这样能方便的找到答案。比如说你看要看 tensorflow/python/platform/flags.py 其实是个披着很薄的装饰的argparse.ArgumentParse()(什么是python的argparse,请看这里 点击打开链接 ),所以也还是得看python的argparse的API文档。 好吧,在我辛苦弄懂argparse也就是一个方便用户添加命令行的玩意之后,问题终于明了了。 我抽取了flags.py的几段程序 这里是import了argparse as _argparse 然后建立argparse一个全局对象_global_parser 定义了_FlagValues这个类,并使用前面定义的全局对象_global_parser,将