argparse

Open compressed file directly with argparse

无人久伴 提交于 2020-01-07 03:06:04
问题 Can I open a gzip file directly with argparse by changing the type=argparse.FileType() to some gzip type? It's not in the docs, so I'm not sure if argparse even suppoets compressed file types... 回答1: First, the type parameter is a function or other callable that converts a string into something else. That's all. argparse.FileType is factory class that ends up doing something close to: def filetype(astring): mode = 'r' # or 'w' etc. try: f = open(astring, mode) except IOError: raise argparse

Python argparse a list input

拟墨画扇 提交于 2020-01-06 19:06:43
问题 The code below accepts command line arguments for mode such as -m fizz and -m fizz bazz . This, as expected, passes the arguments to the main function as ['fizz', 'bazz'] (for the second example above). It seems user-unfriendly to pass command line arguments with spaces, so I'd like argparse to accept a comma-separated list e.g. -m fizz,bazz or -m ['fizz','bazz'] . How can I modify the code below to do this? Thanks! import agrparse ... parser.add_argument("-m", "--mode", nargs="*", default=[

Python argparse a list input

不羁的心 提交于 2020-01-06 19:06:09
问题 The code below accepts command line arguments for mode such as -m fizz and -m fizz bazz . This, as expected, passes the arguments to the main function as ['fizz', 'bazz'] (for the second example above). It seems user-unfriendly to pass command line arguments with spaces, so I'd like argparse to accept a comma-separated list e.g. -m fizz,bazz or -m ['fizz','bazz'] . How can I modify the code below to do this? Thanks! import agrparse ... parser.add_argument("-m", "--mode", nargs="*", default=[

如何读取/处理命令行参数?

可紊 提交于 2020-01-06 17:06:40
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我原本是C程序员。 我看到了许多花招和“技巧”来阅读许多不同的论点。 Python程序员可以通过哪些方式做到这一点? 有关 获取/解析传递给Python脚本的命令行参数的最佳方法是什么? 实现“ [命令] [动作] [参数]”样式的命令行界面? 如何在Python中处理命令行参数? 如何使用Python的optparse格式化位置参数帮助? #1楼 标准库中的规范解决方案是 argparse ( docs ): 这是一个例子: from argparse import ArgumentParser parser = ArgumentParser() parser.add_argument("-f", "--file", dest="filename", help="write report to FILE", metavar="FILE") parser.add_argument("-q", "--quiet", action="store_false", dest="verbose", default=True, help="don't print status messages to stdout") args = parser.parse_args() argparse 支持(除其他外):

Argparse可选的位置参数?

百般思念 提交于 2020-01-06 16:37:06
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我有一个打算像这样 usage: installer.py dir [-h] [-v] 的脚本: usage: installer.py dir [-h] [-v] dir 是一个位置参数,其定义如下: parser.add_argument('dir', default=os.getcwd()) 我希望 dir 是可选的:未指定时,应该只是 cwd 。 不幸的是,当我不指定 dir 参数时,出现 Error: Too few arguments 。 #1楼 作为@VinaySajip答案的扩展。 还有其他值得一提的 nargs 。 parser.add_argument('dir', nargs=1, default=os.getcwd()) N(整数)。 命令行中的N个参数将一起收集到一个列表中 parser.add_argument('dir', nargs='*', default=os.getcwd()) '*'。 存在的所有命令行参数都收集到一个列表中。 请注意 ,使用 nargs='*' 包含多个位置参数通常没有多大意义,但是可以使用多个使用 nargs='*' 可选参数。 parser.add_argument('dir', nargs='+', default=os.getcwd()) '+'

How to run certain function based on command line input given in python

ぃ、小莉子 提交于 2020-01-06 08:09:13
问题 I have my main script where i have two functions defined. The or_search will find occurrences of a string specified and add to the list what index position it has been found within. The second function and_search finds occurrences of a string specified and a counter is used to increment the amount of times it has been found. In my main function , if i pass for example python main.py andsearch commission , item , sold , it should run the and_search function and bring back the results. It

python argparse - passing “argument” to argument [duplicate]

拜拜、爱过 提交于 2020-01-06 06:53:08
问题 This question already has answers here : Can't get argparse to read quoted string with dashes in it? (4 answers) Closed last year . I'd like to pass an "argument" to argument. I.e., in the following code: import argparse parser = argparse.ArgumentParser() parser.add_argument("-a") print parser.parse_args(['-a', 'hi']) print parser.parse_args(['-a', '-hi']) The output is: Namespace(a='hi') usage: problem.py [-h] [-a A] problem.py: error: argument -a: expected one argument While I'd like it to

parse_args all .png files from a parser argument

喜欢而已 提交于 2020-01-06 06:28:30
问题 I would like to get a arg.pics which returns something like ['pic1.png', 'pic2.png', 'pic3.png'] (to arbitrarily parse all files of .png format) after running the following ( test.py ): import argparse import os def parser_arg(): par = argparse.ArgumentParser() parser = par.add_argument_group('pictures') parser.add_argument("-p", "--pics", nargs="+", help="picture files", required=True) arguments = par.parse_args() return arguments args = parser_arg() And upon running the script via command

Nested ArgumentParser

可紊 提交于 2020-01-05 12:07:39
问题 I'm trying to build nested parsers for a command line tool. I'm currently using add_subparsers , but it seems not powerful enough for one specific case. I cannot add same named arguments to both the parent parser and subparser commands. See the following example: import argparse argparser = argparse.ArgumentParser() argparser.add_argument("-H", action="store_true") subparser = argparser.add_subparsers(dest='sp') cmd = subparser.add_parser("cmd") cmd.add_argument("-H") print argparser.parse

Create parser with subcommands in argparse, customize positional argument(s)

╄→гoц情女王★ 提交于 2020-01-05 08:33:22
问题 I'm very new to this module so please bear with me. I have the following code: reader.py import argparse parent_parser = argparse.ArgumentParser(description="Read text files.") parent_parser.add_argument('filename', help='TXT file', type=file, nargs='+') parent_parser.add_argument('--verbose', '-v', action='store_true', help="Verbosity on") child_parser = parent_parser.add_subparsers(title="subcommand", help="Subcommand help") new_file_command = child_parser.add_parser('new', help="New text