getopt
ï¼åCä¸çgetopt()çä»·ã
optparse
ï¼2.7åå·²ä¸æ¨è使ç¨ã
argparse
ï¼åºäºoptparseçæ°åºã
docopt
ï¼æ ¹æ®ææ¡£æè¿°ï¼èªå¨çæãå¦ä¸ä»½åèææ¡£ï¼
docopt
ã
æ´è¯¦ç»çå 容å¯åèä¸è¿°ææ¡£ã
getopt
è¥å¯¹Cç
getopt()
å½æ°ä¸çæï¼æè
å¾åäºä½¿ç¨è¾å°ç代ç ï¼æè
éè¦å¯¹å¸®å©ä¿¡æ¯åé误信æ¯ææ´é«è¦æ±ï¼ä»¥ä¸æ
åµä¼å
使ç¨
argparse
ã
该模å主è¦æä¾ä¸¤ä¸ªå½æ°ï¼ä»¥åä¸ä¸ªå¼å¸¸å¤æã
getopt.
getopt
(
args
,
options
[,
long_options
])
args
æ¯éè¦è§£æçå表ï¼é常æ
åµä¸ï¼æ¯
sys.argv[1:]
ã
options
æ¯è§£æå表æ¶ä½¿ç¨çèå¼ï¼å¦ææä¸é¡¹ï¼å个åæ¯ï¼åé¢éè¦åæ°ï¼åå¨æ¤é¡¹åæ·»å
':'
ã
long_options
å¿
é¡»æ¯å符串å表ï¼å符串å¼å¤´ç
'--'
ä¸è½å
æ¬ï¼è¥æä¸é¡¹åé¢éè¦åæ°ï¼åå¨æ¤é¡¹åæ·»å
'='
ãå¯éåçåæ°æ¯ä¸æ¯æ
long_options
çãæ¯æ ¹æ®è¾å
¥æ¯å¦æ¯å符串å表ä¸å¯ä¸ä¸é¡¹çåç¼æ¥å¹é
çã
è¿åå¼å为两个é¨åã第ä¸é¨åæ¯
(option, value)
æ ¼å¼çå表ï¼ç¬¬äºé¨åæ¯è§£æå®åå©ä½é¨åã
getopt.
gnu_getopt
(
args
,
options
[,
long_options
])
ä½ç¨åä¸ã
åºå«å¨äº
getopt()
ä¸æ¦éå°æªé
ç½®çé项ï¼å°±ä¼åæ¢è§£æãèæ¤å½æ°ä½¿ç¨GNUè§åï¼å³å·²é
ç½®çé项åæªé
ç½®çé项æ¯å¯ä»¥æ··æçã
exception
getopt.
GetoptError**
å½ä¸ä¸ªæ æ³è¯å«çé项ï¼æè
ä¸ä¸ªå¿
é¡»è¦æåæ°ä¼ å
¥çé项æªä¼ å
¥åæ°æ¶ï¼æåºæ¤å¼å¸¸ã
exception
getopt.
error**
GetoptError
çæ§åï¼ä¸ºäºååå
¼å®¹èä¿çã
å ·ä½å®ä¾å¯åèææ¡£ï¼ä¸æ¨è使ç¨ã
argparse
ä¸ä¸ªç®åä¾åï¼
import argparse
parser = argparse.ArgumentParser(description='Example with long option names')
parser.add_argument('--noarg', action="store_true", default=False)
parser.add_argument('--witharg', action="store", dest="witharg")
parser.add_argument('--witharg2', action="store", dest="witharg2", type=int)
print parser.parse_args(['--noarg', '--witharg', 'val', '--witharg2=3'])
å¨argparseä¸ï¼æ常ç¨çå°±æ¯ä¸è¿°ä¸é¨åäºï¼å建ä¸ä¸ªArgumentParser
对象ï¼ä½¿ç¨add_argument()
æ¹æ³æ¥ä¸ºå建çArgumentParser
对象添å argumentç解æè§åï¼æåè°ç¨parse_args()
æ¥è§£æä¼ å
¥çå
容ï¼ä¾æ®çæ¯ç¬¬äºæ¥å¶å®çè§åï¼çæçæ¯ä¸ä¸ªNamespace
对象ï¼è¥æªä¼ åæ°ç»parse_args()
ï¼é£ä¹é»è®¤ä»sys.argv
æ¥è·åå½ä»¤è¡å
¥åã
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)
prog
ï¼ç¨åºçååï¼é»è®¤æ¯argv[0]ãè¥è®¾ç½®ï¼åå¨å¸®å©ä¿¡æ¯ä¸ï¼å¯ä»¥ä½¿ç¨%(prog)s
æ¥ä½ä¸ºæ ¼å¼åçå¼ç¨ï¼ä¿®æ¹ä¸å¤å
¨å±åç¨ï¼ã
usage
ï¼å¸®å©ä¿¡æ¯çusageå段ï¼æè¿°ç¨åºçåç§ç¨æ³ï¼é»è®¤æ
åµä¸ä¼ä¾æ®add_argument()
æ¥èªå¨çæã
description
ï¼ä¸ä¸ªç®åæè¿°ç¨åºä¸»è¦å¹²å¥ä»¥åæä¹ç¨çå符段ãä½äºusageå符段åoptional argumentså符段ä¹é´ã
epilog
ï¼optional argumentså符段ä¹åçå符段ã
parents
ï¼ç»§æ¿çç¶parserï¼ä¸ºäºé¿å
ä¸äºå
Œ
±çå
容éå¤å®ä¹ï¼ç¶parserå¨åå§åæ¶ä¼è®¾ç½®add_help=False
ï¼è¿æ¯ä¸ºäºé²æ¢åºç°ç¶ä¸åparserç-h
å²çªèæåºå¼å¸¸ã
formatter_class
ï¼å¯¹äºhelpè¾åºè¿è¡æ ¼å¼åï¼é¤äºè¾åºçæ ·å¼å¤ï¼å¦æ设置为ArgumentDefaultsHelpFormatter
ï¼åä¼èªå¨å¨helpè¾åºä¸æ·»å å·²å®ä¹çdefaultå¼ã
prefix_chars
ï¼optionsåçå符ï¼é»è®¤ä¸º'-'ï¼å¯ä»¥æ·»å å
¶ä»å符ï¼å¦'-+'ï¼ä½æ¯å¦æ没æå
æ¬'-'ï¼é£ä¹å¯¹åºçoptionå¦'-h'å°±æ æ³è§£æã
fromfile_prefix_chars
ï¼ææ¶ä¼ä½¿ç¨æ件ç»parse_args()
ä¼ å
¥åæ°ï¼ä¸ºäºè½å¤è¯å«æ件å符串ï¼å¦"demo.txt"ï¼éè¦è®¾ç½®æ¤å¼ï¼å¦"@"ï¼é£ä¹ææ以æ¤å符为å¼å¤´çå符串é½è¢«å½ä½æ¯æ件ï¼æä»¥ä¼ ç»parse_args()
çåæ°åºè¯¥æ¯@demo.txt
ãå¨è¯¥æ件ä¸ï¼ä¸è¡åªè½æä¸ä¸ªåæ°ãå¦æ件ä¸ç'-f\nbar'
ä¼è¢«è§£ææ['-f','bar']
ã
argument_default
ï¼ä¸è¬æ
åµä¸ï¼é»è®¤å¼ä½¿ç¨add_argument()
æ¥æ·»å ï¼æè
使ç¨set_defaults()
设置ä¸äºé®å¼å¯¹æ¥æ·»å ãå©ä¸ä¸ç§æ
åµå°±æ¯è®¾ç½®æ¤é¡¹ï¼æ¤å¤æ²¡çæç½æ¯ååäºï¼ã
conflict_handler
ï¼è§£å³å¨add_argument()
é¶æ®µæå²çªçoptionçä¾æ®çç¥ï¼é»è®¤ä¸ºerror
å³æåºå¼å¸¸ãä¸è¬æ
åµä¸éå°å²çªæ¯æåºå¼å¸¸å³å¯ï¼ä½æ¯å¦æ设置äºparents
ï¼é£ä¹éè¦éåç¶parserä¸çè§åçæ¶åï¼å°±éè¦å°æ¤é¡¹è®¾ç½®ä¸ºresolve
ï¼ä½æ¯éåæ¯ç²¾ç¡®å¹é
çï¼å¦èè§åå®ä¹äº-h/--help
ï¼éåäº-h
ï¼é£ä¹--help
è¿æ¯èè§åã
add_help
ï¼æ¯å¦æ·»å -h/--help
optionï¼é»è®¤ä¸ºTrueã为Falseæ¶ï¼æ¯è¦åparentï¼å
¶å®å¯ä»¥è®¾ç½®åParseréåï¼ãé»è®¤æ¯-h/--help
ï¼è¥prefix_chars
ä¸æ²¡æå
å«'-'ï¼é£ä¹å°±ä»¥å
¶ä¸ç¬¬ä¸ä¸ªå符ä½ä¸ºä»£æ¿ã
ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][,metavar][, dest])
name or flags
ï¼æ¯ä½ç½®åæ°ï¼åéè¦ä¼ å
¥ååï¼è¦æ¯å¯éåæ°ï¼åéè¦è¿è¡å®ä¹ï¼å¦'-f'ï¼'--foo'ã
action
ï¼å®ä¹ä¼ å
¥çåæ°å¦ä½å¤çã
action='store'
ï¼é»è®¤åå¼ï¼ä¿åä¼ å
¥åæ°ã
action='store_const'
ï¼éè¦æ·»å const
ï¼æå³ç该argumentçå¼ä¸ä»å½ä»¤è¡è¾å
¥ï¼èæ¯åconst
çå¼ã
action='store_true' or action='store_false'
ï¼'store_const'
çç¹æ®æ
å½¢ï¼æå³çconst
çå¼ä¸ºTrue
æFalse
ã
action='append'
ï¼è¡¨ç¤ºä¼ å
¥çå¼ä¼ä½ä¸ºä¸ä¸ªå表çä¸é¡¹ï¼æå³çoptionå¯ä»¥å¨å½ä»¤è¡ä¸å¤æ¬¡åºç°ã
action='append_const'
ï¼ä¼ å
¥å表ç项ç±const
å®ä¹ï¼é常ç¨å¨éè¦å¤ä¸ªargumentå°å¼ä¼ å
¥ä¸ä¸ªå表ä¸çåºæ¯ã
action='count'
ï¼è¾åºargumentåºç°ç次æ°ã
action='help'
ï¼å·²é»è®¤æ·»å ã
action='version'
ï¼éè¦å®ä¹version
ï¼ä½¿ç¨æ¶è¾åºçæ¬ä¿¡æ¯å¹¶éåºã
èªå®ä¹ï¼éè¿å®ä¹ä¸ä¸ªargparse.Action
åç±»æ¥å®ç°ãå®é
ä¸ï¼ä¸é¢çè¿äºå¯é项é½æ¯éè¿è¿ç§å½¢å¼å®ä¹çã
nargs
ï¼é常ä¸ä¸ªé项åè·ä¸ä¸ªåæ°ï¼éè¿è®¾ç½®æ¤é¡¹å¯ä»¥å®ç°ä¸åæ
åµã
nargs=N
ï¼ä¸ä¸ªé项åå¯ä»¥è·å¤ä¸ªåæ°ï¼action='append'
æ¶ï¼ä¾ç¶æ¯ä¸ä¸ªé项åè·ä¸ä¸ªåæ°ï¼åªä¸è¿é项å¯ä»¥å¤æ¬¡åºç°ï¼ï¼åæ°ç个æ°å¿
须为N
çå¼ï¼è¿äºåæ°ä¼çæä¸ä¸ªå表ï¼å½nargs=1
æ¶ï¼ä¼çæä¸ä¸ªé¿åº¦ä¸º1çå表ã
nargs=?
ï¼å¦æ没æå¨å½ä»¤è¡ä¸åºç°å¯¹åºç项ï¼åç»å¯¹åºç项èµå¼ä¸ºdefault
ãç¹æ®çæ¯ï¼å¯¹äºå¯é项ï¼å¦æå½ä»¤è¡ä¸åºç°äºæ¤å¯é项ï¼ä½æ¯ä¹å没æè·éèµå¼åæ°ï¼åæ¤æ¶ç»æ¤å¯é项并ä¸æ¯èµå¼default
çå¼ï¼èæ¯èµå¼const
çå¼ã
nargs=*
ï¼åN
类似ï¼ä½æ¯æ²¡æè§å®å表é¿åº¦ã
nargs=+
ï¼å*
类似ï¼ä½æ¯ç»å¯¹åºç项å½æ²¡æä¼ å
¥åæ°æ¶ï¼ä¼æ¥éerror: too few arguments
ã
nargs=argparse.REMAINDER
ï¼ææå©ä½çåæ°ï¼å转å为ä¸ä¸ªå表èµå¼ç»æ¤é¡¹ï¼é常ç¨æ¤æ¹æ³æ¥å°å©ä½çåæ°ä¼ å
¥å¦ä¸ä¸ªparserè¿è¡è§£æã
å¦ænargs
没æå®ä¹ï¼åå¯ä¼ å
¥åæ°çæ°éç±action
å³å®ï¼é常æ
åµä¸ä¸ºä¸ä¸ªï¼å¹¶ä¸ä¸ä¼çæé¿åº¦ä¸ºä¸çå表ã
const
ï¼ä¸ç§æ¯å®ä¹action='store_const'
æaction='append_const'
æ¶ä½¿ç¨ãä¸ç§æ¯å®ä¹nargs='?'
æ¶ï¼å¯é项åºç°å¨å½ä»¤è¡ä¸ï¼ä½ä¹å并没æè·éèµå¼çåæ°ï¼ä½ä¸ºé»è®¤å¼ä¼ ç»æ¤å¯é项ã
default
ï¼é»è®¤å¼ã
å¦ææ¯ä¸ä¸ªå符串ï¼é£ä¹Parser解æçæ¶åä¼å°å®ä½ä¸ºå½ä»¤è¡ä¼ å
¥å¼ï¼ä½¿ç¨type
çå¼æ¥è¿è¡è½¬æ¢ç±»åï¼ä½æ¯å¦æä¸æ¯çè¯ï¼å°±ä¼ä½¿ç¨å®ä¹çå¼èä¸è¿è¡ç±»å转æ¢ã
å¦æ设置äºnargs='?'
ænargs='*'
ï¼é£ä¹å½æ²¡æåæ°èµå¼ç»è¯¥é¡¹æ¶ï¼ä¼ä½¿ç¨default
å®ä¹çå¼ã
èdefault=argparse.SUPPRESS
æ¶ï¼å表示å½ä»¤è¡ä¸æªåºç°æä¸é¡¹æ¶ï¼ä¸ä¼å¯¹å®è¿è¡é»è®¤èµå¼ã
type
ï¼ç¨äºç±»åæ£æ¥åç±»å转æ¢ã
使ç¨FileType
å¯ç®å对æ件çæä½ã
è¿å¯ä»¥èªå®ä¹å½æ°ï¼è¾å
¥æ¯ä¸ä¸ªå符串ï¼è¾åºæ¯è½¬æ¢åçå符串ã
å½è®¾ç½®choices
çæ¶ï¼ç±»åæ£æ¥ä¼åå¾å®¹æï¼å 为åªéè¦å¨ä¸ä¸ªèå´å
æ¯è¾å³å¯ã
choices
ï¼ç»å®äºåå¼èå´ï¼è¶
åºä¼æ¥éã
å½type
ä¹æå®ä¹æ¶ï¼ä¼å
使ç¨type
è¿è¡ç±»åæ£æ¥ï¼æ以choices
ä¸çåå¼å¿
须符åtype
çå®ä¹ï¼å¦åå¨parse_args()
æ¶ä¼æ¥éã
ä»»ä½æ¯æin
æä½ç¬¦çåå¯ä½ä¸ºchoices
çèµå¼ï¼æ以åå
¸ï¼å表ï¼éåï¼ççå
¶ä»å®¹å¨åé½æ¯æã
required
ï¼é»è®¤æ
åµä¸ï¼å¯é项ï¼åé¢æ'-'
ï¼è¢«è®¤ä¸ºå¹¶ä¸ä¸å®éè¦åºç°å¨å½ä»¤è¡åæ°ä¸ï¼ä½æ¯å¦æ设置äºrequired=True
çè¯ï¼åå¿
é¡»åºç°ãæ¤ç±»è®¾ç½®è¿è人ç常è¯ï¼åºé¿å
使ç¨ã
help
ï¼å¸®å©ä¿¡æ¯ã
ä¹åæå°ç%(prog)s
å¯ç¨äºæ¤å¤ç¨åºåçæ ¼å¼åï¼æ¤å¤ï¼è¿æ%(default)s
æ ¼å¼ådefault
çå¼ï¼%(type)s
æ ¼å¼åtype
çå¼ã
设置为argparse.SUPPRESS
å¯ä¸æ¾ç¤ºå¸®å©ä¿¡æ¯ã
metavar
ï¼å¨Parserçæ帮å©ä¿¡æ¯æ¶ï¼éè¦æå符代表éè¦ä¼ å
¥çå¼ãï¼è¿ä¸æ®µådest
ç¸åï¼ä½¿ç¨çå°±æ¯dest
çå¼ï¼å¦ææ¯ä½ç½®åæ°ï¼åç¨å®æ¬èº«ä»£æ¿ï¼å¦ææ¯å¯éåæ°ï¼å使ç¨å®ç大åæ¥ä»£æ¿ã使ç¨metavar
å¯æ¿æ¢é»è®¤çå符ã
dest
ï¼å¤§é¨åçé项é½éè¦éè¿å½ä»¤è¡æ¥ç»å
¶èµå¼ï¼è¿äºå¼çååéè¿dest
æ¥å®ä¹ï¼é»è®¤çè§åå¦åmetavar
ä¸æè¿°ã
ArgumentParser.parse_args(args=None, namespace=None)
å°args
转æ¢ä¸ºnamespace
对象çä¸ä¸ªå¼ãé»è®¤æ
åµä¸ï¼sys.argvèµå¼ç»args
ï¼ä¸ä¸ªç©ºçNamespace
对象ä¼è¢«å建ã
解ææ¶ï¼ä¼å¯¹ä¼ å
¥çåæ°è¿è¡æ£æ¥ï¼è¥ä¸ç¬¦åè¦æ±å°±ä¼æ¥éã
ä¸è¬æ
åµä¸ï¼ä¼èªå¨å¤æä¼ å
¥çå¼å°åºæ¯ä¸ä¸ªå¯éåæ°ï¼è¿æ¯ä¸ä¸ªè´æ°ï¼é½ç¨'-'å¼å¤´ï¼ãä½ææ¶ä½ç½®åæ°çå¼å¿
é¡»æ¯ä¸ä¸ª'-'å¼å¤´çå¼ï¼å¦'-f'
ï¼é£ä¹ä½¿ç¨parser.parse_args(['--', '-f'])
ï¼'--'
代表åç»çææä¼ å
¥å¼é½éè¦çåæ¯ä½ç½®åæ°ã
parse_args()
ä¼è¿åå¡«å
好çNamespace
对象ï¼è¥è¦å°å®å为åå
¸ï¼å¯ä½¿ç¨Pythonèªå¸¦çvars()
ã
vars(...)
vars([object]) -> dictionary
Without arguments, equivalent to locals().
With an argument, equivalent to object.__dict__.
ä¹å¯ä¸ä½¿ç¨Namespace
ï¼èæ¯ä¼ å
¥ä¸ä¸ªæåçæç对象ã
ArgumentParser.add_subparsers([title][, description][, prog][, parser_class][, action][, option_string][, dest][, help][, metavar])ç¨æ¥å®ä¹åParserã
class argparse.FileType(mode='r', bufsize=None)ç¨æ¥ç»add_argument()
ä¸çtype
åæ°èµå¼ã
ArgumentParser.add_argument_group(title=None, description=None)å®ä¹ä¸ä¸ªç»ã
ArgumentParser.add_mutually_exclusive_group(required=False)å®ä¹ç»ä¸åªè½æä¸ä¸ªé项åºç°ã
ArgumentParser.set_defaults(kwargs)设置é»è®¤å¼ã
ArgumentParser.get_default(dest)è·åé»è®¤å¼ã
ArgumentParser.print_usage(file=None)
ArgumentParser.print_help(file=None)
ArgumentParser.format_usage()
ArgumentParser.format_help()以ä¸åç§é½æ¯å¸®å©ä¿¡æ¯ç¸å
³çã
ArgumentParser.parse_known_args(args=None, namespace=None)åªè§£æç¥éçé¨åã
ArgumentParser.convert_arg_line_to_args(arg_line)
ArgumentParser.exit(status=0, message=None)
ArgumentParser.error(message)
docopt
ç念æ¯å¥½ç帮å©ä¿¡æ¯åºè¯¥æ£å¥½å
å«çæå½ä»¤è¡è§£æå¨æéè¦çå
¨é¨ä¿¡æ¯ã
docopt.docopt(doc, argv=None, help=True, version=None, options_first=False)
doc
åæ°å¯ä»¥æ¯ä¸ä¸ªæ¨¡åçdocstring(__doc__
)ï¼æè
æ¯å
¶ä»ç¬¦åæ ¼å¼ç帮å©ä¿¡æ¯ã
argv
é»è®¤æ
åµä¸ä½¿ç¨sys.argv[1:]
ï¼æè
å¯ä»¥ä½¿ç¨ä¸ä¸ªå符串ã
help
é»è®¤ä¸ºTrueï¼å½è¾å
¥-h
æ--help
åï¼éæå设置好ï¼ï¼è§£æå¨è½å¤èªå¨çæ对åºç帮å©ææ¬ãéè¦æå¨ç®¡ççå¯è®¾ç½®ä¸ºFalseã
version
çæ¬ä¿¡æ¯ï¼æ¯å¯éåæ°ã
options_first
é»è®¤ä¸ºFalseãå½è®¾ç½®ä¸ºTrueæ¶ï¼ä¸å
许å¯éåæ°åä½ç½®åæ°è¿è¡æ··åï¼å³å¨åºç°ç第ä¸ä¸ªä½ç½®åæ°ä¹åçææåæ°ï¼å被å½ä½æ¯ä½ç½®åæ°ï¼è¿æ¯ä¸ºäºåPOSIXä¿æå
¼å®¹ã
è¿åå¼æ¯ä¸ä¸ªåå
¸ï¼keyæ¯optionï¼valueå°±æ¯å¯¹åºè¾å
¥çåæ°ã
帮å©ä¿¡æ¯æ ¼å¼å为两é¨åï¼Usage pattern
åOption descriptions
ãåªæ符åæ ¼å¼çå符串æä¼è¢«è¯å«å¹¶è§£æï¼å
¶ä½å符串ä¼è¢«å¿½ç¥ã
Usage pattern
Usage: my_program.py [-hso FILE] [--quiet | --verbose] [INPUT ...]
Usage patternæ¯doc
çåå符串ãå¼å§äºusage:
(大å°åä¸ææ)ï¼ç»æäºä¸è¡ç©ºè¡ã
ä½äºusage:
ä¹åç第ä¸ä¸ªåè¯æ¯ç¨åºçååï¼å¯ä»¥éå¤å¤æ¬¡è¿è¡ä¸åpatternçå®ä¹ã
Usage: my_program.py FILE
my_program.py COUNT FILE
æ¯ä¸ªpatterné½ç±ä»¥ä¸é¨åç»æã
ä¸ä¸ªæ¯<arguments>, ARGUMENTS
æ ¼å¼çå®ä¹ï¼è¦ä¹æ¯ç¨å°æ¬å·å
æ¬ï¼è¦ä¹æ¯å¤§åã
è¿æä¸ä¸ªæ¯-options
ï¼å¿
é¡»è¦ä»¥â-âå¼å¤´ï¼å¯ä»¥å æ 好å 个optionï¼å¦-oiv
çä»·äº-o -i -v
ãoptionæ¯å¯ä»¥æåæ°çï¼å¦-f FILE
ï¼-f=FILE
ï¼-fFILE
ï¼é½æ¯çä»·çãå
¶ä»ä¸äºéæ©å¨ä¸æ¹çoption descriptions
ä¸è¿è¡å
·ä½å®ä¹ã
æåä¸ä¸ªæ¯commands
ï¼é¤äºä»¥ä¸æåç两ç§æ
åµï¼å©ä¸çæ ¼å¼ï¼åå ä¸ä¸¤ä¸ªç¹æ®çå½ä»¤ï¼å个çâ-âåå个çâ-âï¼è¿äºé½å½å±äºæ¤ã
å¨commands
ä¸ï¼[ ]表示å¯éã( )表示å¿
é¡»åå¨ï¼ææ没æç¨[ ]å
裹çå符串é½é»è®¤è¢«( )å
裹ã
|æ¯ç®¡é符ï¼è¥æä¸ä¸ªé项å¿
é¡»åå¨ï¼åå°ä»ä»¬ç¨( )å
裹ï¼å¦my_program.py(--clockwise | --counter-clockwise) TIME
ï¼è¥é½æ¯å¯é项ï¼åç¨[ ]å
裹ï¼å¦my_program.py[--left | --right]
ã
...表示æä¸ä¸ªæå¤ä¸ªå
ç´ ï¼å¦my_program.py FILE ...
表示ä¸ä¸ªæå¤ä¸ªFILEå¯æ¥åï¼èmy_program.py [FILE ...]
表示0个æå¤ä¸ªFILEå¯æ¥åã
[options]ï¼å¤§å°åææï¼æ¯ä»»æoptionsç缩åãå¯ä»¥ä½¿ç¨å®æ¥å®ä¹è¯¥patternå¯ä»¥ä½¿ç¨ä»»ä½å¨ä¸æ¹Option descriptions
ä¸å®ä¹çoptionã
[--]æ¯ä¸ºäºåéå¯éåæ°åä½ç½®åæ°ï¼å¦Usage: my_program [options] [--] <file>...
ï¼å
¶ä¸ä¸æ¬å·å¯å»é¤ï¼å为å¿
填项ã
[-]è¥åºç°ï¼å表示ç¨åºéè¦ä½¿ç¨stdinï¼ç¦æ¢ä½¿ç¨fileãèå个ç-åæ¯ä¸ä¸ªæ®éå符ã
å¦æå¨usage patternä¸éå¤åºç°ååä½ç½®åæ°å¤æ¬¡ï¼æè
ååæåæ°çå¯éåæ°å¤æ¬¡ï¼é£ä¹ä¼ å
¥çå¼ä¼è¢«è§£ææä¸ä¸ªå表ãå¦Usage: my_program.py <file> <file> --path=<path>...
çç»æä¼æ¯args['<file>'] == ['file1', 'file2']
以åargs['--path'] == ['./here', './there']
ã
Option descriptions
-h --help show this
-s --sorted sorted output
-o FILE specify output file [default: ./test.txt]
--quiet print less text
--verbose print more text
æ¤é¨åä½äºusage patternä¸æ¹ï¼å®å¯ä»¥æä¸æ¹é¢ä½ç¨ï¼å®ä¹åä¹ççoptionåé¿optionï¼æ个optionæ¯å¦éè¦ä¼ å
¥åæ°ï¼æ个optionæ¯å¦æé»è®¤åæ°ã
è§åå¦ä¸ï¼
æ¯è¡å®ä¹ä¸ä¸ªoptionï¼å¿
须以ä¸ä¸ªæ两个â-âå¼å¤´ã
为å®ä¹éè¦ä¼ å
¥çåæ°ï¼å¯ä»¥å¨å¯¹åºçoptionåæ¹å ä¸ä¸ä¸ªå符串ï¼ä¸¤è
ä»¥ç©ºæ ¼æâ=âç¸éï¼ä¸¤è
é½æ¯å¯è¡çï¼ä½æ¨èåªä½¿ç¨ä¸ç§é£æ ¼ã
-o FILE --output=FILE # without comma, with "=" sign
-i <file>, --input <file> # with comma, wihtout "=" sign
使ç¨ä¸¤ä¸ªç©ºæ ¼æ¥å°optionæ¬èº«ä¸å®çæè¿°æ³åéï¼æç
§ç¤ºä¾åºè¯¥æ¯è³å°ä¸¤ä¸ªç©ºæ ¼ï¼ã
--verbose More text. # BAD, will be treated as if verbose option had
# an argument "More", so use 2 spaces instead
-q Quit. # GOOD
-o FILE Output file. # GOOD
--stdout Use stdout. # GOOD, 2 spaces
å¨option对åºçæè¿°åæ¹è¿è¡é»è®¤åæ°ç设置ï¼æ ¼å¼æ¯[default:<my-default-value>]
ã
--coefficient=K The K coefficient [default: 2.95]
--output=FILE Output file [default: test.txt]
--directory=DIR Some directory [default: ./]
è¥æoptionæ¯å¯éå¤çï¼åé»è®¤åæ°ä¼ä»¥ç©ºæ ¼ä½ä¸ºåå²ç¬¦è¿è¡åå²ï¼çæä¸ä¸ªå符串å表ãå¦åï¼ä½ä¸ºä¸ä¸ªå符串æ´ä½ã
docoptä¸éç¨äºå¤§åå¤å±æ¬¡çå½ä»¤è¡åæ°è§£æï¼ä¾å¦gitï¼ä½æ¯å®æ¹ç»åºäºä¸ä¸ªexampleï¼ãåæ¶å®æ²¡æ对æ°æ®çæ ¡éªåè½ï¼æ æ³åç¨æ·æ¥éã
使ç¨try docoptå¯è¿è¡docoptå¨çº¿å½ä»¤è¡åæ°è§£æã
ä¾é¢æ¯ï¼ç¥éåå®é¢è¯é¢ã
使ç¨argparseï¼
import argparse
from argparse import ArgumentDefaultsHelpFormatter
parser = argparse.ArgumentParser(prog="Spider", description='Knownsec Interview Spider By Docopt.', formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument('-u', dest='url', action='store', required=True, help='the url the spider begins with')
parser.add_argument('-d', dest='deep', action='store', required=True, type=int, help='the depth of spider')
parser.add_argument('-f', dest='logfile', action='store', default='spider.log', help='the name of logfile')
parser.add_argument('-l', dest='loglevel(1-5)', action='store', type=int, choices=range(1,6), default=1, help='the details level of the log, range 1 to 5')
parser.add_argument('--testself', dest='testself', action='store_true', help='the self test by this program')
parser.add_argument('--thread', dest='thread_number', action='store', type=int, default=10, help="the thread pool's capacity")
parser.add_argument('--dbfile', dest='dbfile_filepath', action='store', required=True, help='the sqlite3 file path')
parser.add_argument('--key', dest='keyword', action='store', help='the keyword to search in webpage')
print parser.parse_args()
python argparse_demo.py -u 123.com -d 1 --dbfile 123.txt -f logfile -l 1 --testself --thread 2
Namespace(dbfile_filepath='123.txt', deep=1, keyword=None, logfile='logfile', loglevel(1-5)=1, testself=True, thread_number=2, url='123.com')
ä½ä¼å°±æ¯åè½å¾å¼ºå¤§ï¼å®ç°å¾æ¹ä¾¿ãæ以æå¾åäºä½¿ç¨å®ã
使ç¨docoptï¼
Knownsec Interview Spider By Docopt.
Usage:
docopt_demo.py -u <url> -d <deep> [-f <logfile>] [-l <loglevel>] [--testself] [--thread <number>] --dbfile <filepath> [--key=<keyword>]
docopt_demo.py -h | --help
docopt_demo.py --version
Options:
-h --help Show this screen.
--version Show version.
-u <url> the url the spider begins with
-d <deep> the depth of spider
-f <logfile> the name of logfile [default: spider.log]
-l <loglevel> the details level of the log, range 1 to 5 [default: 1]
--testself the self test by this program
--thread <number> the thread pool's capacity [default: 10]
--dbfile <filepath> the sqlite3 file path
--key=<keyword> the keyword to search in webpage
from docopt import docopt
if __name__ == '__main__':
arguments = docopt(__doc__, version='Spider 1.0')
print arguments
python docopt_demo.py -u 123.com -d 1 --dbfile 123.txt -f logfile -l 1 --testself --thread 2
{'--dbfile': '123.txt',
'--help': False,
'--key': None,
'--testself': True,
'--thread': '2',
'--version': False,
'-d': '1',
'-f': 'logfile',
'-l': '1',
'-u': '123.com'}
å¯è§ï¼å¦æé项ä¸éè¦åæ°ï¼å-hå--versionè¿ç§æ²¡æ设置çä¼èµå¼ä¸ºFalseï¼è--testselfä¼èµå¼ä¸ºTrueï¼å¯¹äºéè¦åæ°çé项ï¼å¦æ没æ设置ï¼å¦--keyï¼ä¼è¢«èµå¼ä¸ºNoneï¼èæ设置并ä¸ç»åºåæ°çï¼ä¼èµå¼å¯¹åºçåæ°ï¼å¦--dbfileã