Python 标准库学习之argparse
来源:互联网 发布:微博个性域名二次修改 编辑:程序博客网 时间:2024/05/24 16:13
argparse.ArgumentParser
class argparse.ArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=argparse.HelpFormatter, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True) Create a new ArgumentParser object. All parameters should be passed as keyword arguments. Each parameter has its own more detailed description below, but in short they are:prog - The name of the program (default: sys.argv[0]) usage - The string describing the program usage (default: generated from arguments added to parser) description - Text to display before the argument help (default: none) epilog - Text to display after the argument help (default: none) parents - A list of ArgumentParser objects whose arguments should also be included formatter_class - A class for customizing the help output prefix_chars - The set of characters that prefix optional arguments (default: ‘-‘) fromfile_prefix_chars - The set of characters that prefix files from which additional arguments should be read (default: None) argument_default - The global default value for arguments (default: None) conflict_handler - The strategy for resolving conflicting optionals (usually unnecessary) add_help - Add a -h/–help option to the parser (default: True) allow_abbrev - Allows long options to be abbreviated if the abbreviation is unambiguous. (default: True)
ArgumentParser.add_argument
ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest]) Define how a single command-line argument should be parsed. Each parameter has its own more detailed description below, but in short they are:name or flags - Either a name or a list of option strings, e.g. foo or -f, --foo. action - The basic type of action to be taken when this argument is encountered at the command line. nargs - The number of command-line arguments that should be consumed. const - A constant value required by some action and nargs selections. default - The value produced if the argument is absent from the command line. type - The type to which the command-line argument should be converted. choices - A container of the allowable values for the argument. required - Whether or not the command-line option may be omitted (optionals only). help - A brief description of what the argument does. metavar - A name for the argument in usage messages. dest - The name of the attribute to be added to the object returned by parse_args().
目录树2.0
from pathlib import Pathimport sysimport os.pathimport argparsedef getArgs(): parser = argparse.ArgumentParser(prog = 'Dirtree', description = 'This program is mainly programmed to help you to list a directory tree from a given path') parser.add_argument('--path', '-p', help = 'start path', default = '.') parser.add_argument('--smaller', '-s', type = lambda x : int(x) * 1024, help = 'list file smaller than given KB', default = None) parser.add_argument('--larger', '-l', type = lambda x : int(x) * 1024, help = 'list file larger than given KB', default = None) parser.add_argument('--mode', '-m', help='search file fit the given mode', default='*.*') parser.add_argument('--forbid', '-f', help='forbid to list dirtree, just list the abspath', action = 'store_true') args = parser.parse_args() return argsdef filesizeOK(p, s, l): if s and l: return l <= os.path.getsize(p) <= s elif s: return os.path.getsize(p) <= s elif l: return l <= os.path.getsize(p) else: return Truedef lst_tree(p, n, s, l, m, f): if p.is_file() and filesizeOK(p, s, l) and p.match(m): if not f: print('|' + '\t|' * n + '-' * 4 + p.name) else: print(os.path.abspath(p)) elif p.is_dir(): if not f: print('|' + '\t|' * n + '-' * 4 + str(p.relative_to(p.parent)) + '\\') for pt in p.iterdir(): lst_tree(pt, n + 1, s, l, m, f)if __name__ == "__main__": args = getArgs() lst_tree(Path(args.path), 0, args.smaller, args.larger, args.mode, args.forbid)
参数说明:
-p 起始目录
-s 文件最大KB
-l 文件最小KB
-m 搜索符合模式文件
-f 不用目录树,打印绝对路径
阅读全文
1 0
- Python 标准库学习之argparse
- python 标准库-argparse 学习
- python学习之 argparse
- python学习 之 argparse
- python学习之argparse模块
- python学习之argparse模块
- python学习之argparse模块
- python学习之argparse模块
- python学习之argparse模块
- python学习之argparse模块
- python学习之argparse模块
- python学习之argparse模块
- python学习之argparse模块
- Python学习之argparse模块
- python之argparse模块学习
- python学习之argparse模块
- python学习之argparse模块
- python学习之argparse模块
- Shiro框架编码/解码(五)
- React-Native 中 react-navigation 简单
- 用xampp链接mysql出现的问题
- Datasture-1 Stack--用Java实现
- HPU 一只小蜜蜂
- Python 标准库学习之argparse
- lombok(转载)
- Unknown class AvatarView in Interface Builder file.
- 手机号正则表达式
- Python selenium中一定要会用selenium等待的三种等待方式解读
- MySQL二进制日志
- Linux下安装erlang及rabbitmq
- 初探vim编辑器(一)
- 学习oracle的一些知识点记录