argparse的简单使用及注意事项

此模块是受optparse启发的命令行解析库

"""Command-line parsing library

This module is an optparse-inspired command-line parsing library that:

    - handles both optional and positional arguments
    - produces highly informative usage messages
    - supports parsers that dispatch to sub-parsers

The following is a simple usage example that sums integers from the
command-line and writes the result to a file::

    parser = argparse.ArgumentParser(
        description='sum the integers at the command line')
    parser.add_argument(
        'integers', metavar='int', nargs='+', type=int,
        help='an integer to be summed')
    parser.add_argument(
        '--log', default=sys.stdout, type=argparse.FileType('w'),
        help='the file where the sum should be written')
    args = parser.parse_args()
    args.log.write('%s' % sum(args.integers))
    args.log.close()

The module contains the following public classes:

    - ArgumentParser -- The main entry point for command-line parsing. As the
        example above shows, the add_argument() method is used to populate
        the parser with actions for optional and positional arguments. Then
        the parse_args() method is invoked to convert the args at the
        command-line into an object with attributes.

    - ArgumentError -- The exception raised by ArgumentParser objects when
        there are errors with the parser's actions. Errors raised while
        parsing the command-line are caught by ArgumentParser and emitted
        as command-line messages.

    - FileType -- A factory for defining types of files to be created. As the
        example above shows, instances of FileType are typically passed as
        the type= argument of add_argument() calls.

    - Action -- The base class for parser actions. Typically actions are
        selected by passing strings like 'store_true' or 'append_const' to
        the action= argument of add_argument(). However, for greater
        customization of ArgumentParser actions, subclasses of Action may
        be defined and passed as the action= argument.

    - HelpFormatter, RawDescriptionHelpFormatter, RawTextHelpFormatter,
        ArgumentDefaultsHelpFormatter -- Formatter classes which
        may be passed as the formatter_class= argument to the
        ArgumentParser constructor. HelpFormatter is the default,
        RawDescriptionHelpFormatter and RawTextHelpFormatter tell the parser
        not to change the formatting for help text, and
        ArgumentDefaultsHelpFormatter adds information about argument defaults
        to the help.

test_example

#############test the  argparse###################################
import cv2
import numpy as np
import argparse
import glob
import os

standard_height = 512
standard_width = 512
small_height = 256
small_width = 256
region_list = [0]

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        formatter_class= argparse.ArgumentDefaultsHelpFormatter
    )

    parser.add_argument('--source',
                        default = 'P14_100016572_56_r_crop.jpg',
                        help = 'Name of training AOI')
    parser.add_argument('--data_dir',
                        default = '../data/',
                        help = 'folder to image data')
    args = parser.parse_args()

    print("args.source:",args.source)
    print("args.data_dir:",args.data_dir)
   

one
直接右键run检验是否可以成功运行:可以
在这里插入图片描述
two
直接在命令行处运行:python XX.py,看是否成功:可以
在这里插入图片描述
three
在命令行处是否可以修改变量的值?:可以, 等号两边没有空格
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

three 能否 =‘ ’: 可以的
在这里插入图片描述

默认是str类型,但也可以指定其他的值
在这里插入图片描述
命令行中没有显示的变量就使用默认值
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值