1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        詳解Python中argpasrse模塊的基本使用

        共 5085字,需瀏覽 11分鐘

         ·

        2020-07-10 00:23

        3063288dcdb61e212d39fc1b84903e95.webpdeb02549adfc98f85a429f0d165ebd41.webp

        點擊藍(lán)字關(guān)注我們

        51f12501d57be76ae9662c10f8a12259.webp


        9507fe9fd067466d9bcbe2c9b26493f7.webp2d917596fba9d247744450a72dad09a2.webp


        argpasrse模塊的使用


        import  argparseparser = argparse.ArgumentParser(    prog = 'ls',    description='Process some int',    add_help = True)parser.add_argument('path',nargs='?', default='.', help='file path')# 位置參數(shù)parser.add_argument('-l',dest='list', action='store_true')parser.add_argument('-a', '--all', action='store_true')

        args = parser.parse_args() # 解析:如:/etc ---> e t c 把其當(dāng)作一個可迭代對象了,所以可以這樣輸入 ('/etc',)parser.print_help() # windows 使用這個調(diào)試print('-------------------------------------')print( args.all, args.list, args.path) #None None /etc

        在win下,模擬插入位置參數(shù),如 /etc 可以在run-->Edit configuration下或者在:args = parser.parse_args('/etc')



        ① 默認(rèn)會打印幫助信息,默認(rèn)提供 '''


        py文件:

        import  argparseparser = argparse.ArgumentParser(description='Process some int')args = parser.parse_args()parser.print_help() # windows  使用這個調(diào)試


        打印信息:

        usage: homework_解析_9-4.py [-h]
        Process some int
        optional arguments: -h, --help show this help message and exit



        ② ArgumentParser下的內(nèi)容


        def __init__(self,prog=None,描述程序的,sys.args[0]也就是輸入的命令, 如:a.pyusage=None,程序使用方式description=None,epilog=None,parents=[],formatter_class=HelpFormatter,prefix_chars='-',fromfile_prefix_chars=None,argument_default=None, 缺省值conflict_handler='error',add_help=True,默認(rèn)的幫助信息,-h,--help Fals表示取消allow_abbrev=True):



        ③ 取消help


        import  argparseparser = argparse.ArgumentParser(    prog = 'ls',  # 給命令起個名字 ls    description='Process some int',    add_help = False # 改為False)args = parser.parse_args()parser.print_help()?#?windows??使用這個調(diào)試


        打印信息:

        usage: ls
        Process?some?int


        沒有取消的信息:

        usage: ls [-h] # 中括號表示可選,linux特色
        Process some int
        optional arguments:??-h,?--help??show?this?help?message?and?exit



        ④ 必須提供參數(shù),也就是說執(zhí)行l(wèi)s.py的時候,后面要跟 路徑 如:/etc


        import  argparseparser = argparse.ArgumentParser(    prog = 'ls',    description='Process some int',    add_help = True)parser.add_argument('path')#


        打印信息:

        args = parser.parse_args()usage: ls [-h] pathls:?error:?the?following?arguments?are?required:?path



        ⑤ 長選項


        import  argparseparser = argparse.ArgumentParser(    prog = 'ls',    description='Process some int',    add_help = True)parser.add_argument('path')parser.add_argument('-l')parser.add_argument('-a','--all')
        args?=?parser.parse_args()


        打印信息:

        usage: ls [-h] [-l L] [-a ALL] path
        Process some int
        positional arguments: path
        optional arguments: -h, --help show this help message and exit -l L??-a?ALL,?--all?ALL



        ⑥ namespace,sys.argv[1:],沒有提供 ,就是None,提供了,則傳到namespace


        import  argparseparser = argparse.ArgumentParser(    prog = 'ls',    description='Process some int',    add_help = True)parser.add_argument('path')parser.add_argument('-l')parser.add_argument('-a','--all')
        args = parser.parse_args(('/etc',)) # 解析:如:/etc ---> e t c 把其當(dāng)作一個可迭代對象了,所以可以這樣輸入 ('/etc',)parser.print_help() # windows 使用這個調(diào)試print(args)


        打印信息:

        usage: ls [-h] [-l L] [-a ALL] path
        Process some int
        positional arguments: path
        optional arguments: -h, --help show this help message and exit -l L -a ALL, --all ALLNamespace(all=None, l=None, path='/etc')



        ⑦ 獲取參數(shù)對應(yīng)的屬性的值 ,通過namespace,但是參數(shù)如果有--all這樣的,只能用args.all'''


        args = parser.parse_args('/etc'.split()) # 解析:如:/etc ---> e t c  把其當(dāng)作一個可迭代對象了,所以可以這樣輸入 ('/etc',)print(?args.all,?args.l,?args.path)?#None?None?/etc


        打印信息:

        None?None?/etc



        ⑧ -l -a 默認(rèn)必須帶參,如何不帶參'''


        幫助文檔:

            add_argument() method         ?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.         ?dest - The name of the attribute to be added to the object returned by parse_args().parser.add_argument('path',nargs='?')# 位置參數(shù)打印:usage: ls [-h] [-l L] [-a ALL] [path] 此時path 加中括號了,可有可無
        parser.add_argument('path',nargs='?', default='.')# 位置參數(shù),加了一個默認(rèn)值,當(dāng)前目錄
        #### ? 表示可有可無,+ 至少一個,*可以任意個,數(shù)字表示必須指定數(shù)目
        parser.add_argument('-l', action='store_true') # 不需要傳參了


        打印信息:

        usage:?ls?[-h]?[-l]?[-a?ALL]?[path]?????????parser.add_argument('-l', action='store_true')args = parser.parse_args('-l'.split()) print(?args.all,?args.l,?args.path)?


        打印信息:

        因為store_true,所以給了 -l 則打印true,否則為 false

        如果store_false,不給-l 就打印true

        parser.add_argument('-l', action='store_const', const=23)args?=?parser.parse_args('-l'.split())?


        打印信息:

        如果給了,就打印 23,不給,打印None



        ⑨ 每個命令提供一個help信息 '''


        parser.add_argument('path',nargs='?',?default='.',?help='file?path')#?位置參數(shù)


        打印信息:

        ??path????????file?path



        ⑩ 給namespace的屬性提供一個方便使用的名字,如args.l 使用args.list'''

        parser.add_argument('-l',dest='list', action='store_true')print( args.all, args.list, args.path) #None None /etc


        打印信息:

        False?False?.?

        這個名字只是在namespace中用,不會影響命令調(diào)用時的 -l




        ?? - end -??


        微信號 : coding_club

        ●?掃碼關(guān)注我們

        覺得內(nèi)容還不錯的話,給我點個“在看”唄

        ff9c8a7c9a1a5be9ddb8063384e46b00.webp
        54d0fcb24f7920f857b83b42b0062019.webp



        瀏覽 74
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            久久人妻 | 日本三级欧美三级久久久久 | 男人女人真曰批40分钟视 | 中文无码字幕 | 男人添女人下部高潮全视频 | 国产精品色情A级毛片 | 青娱在线视频 | 日日骚视频 | 天天操欧美网站 | 北条麻妃被躁57分钟视频在线 |