ã¯ããã«: ãªãargparseãå¿ èŠãªã®ãïŒ
Pythonã§ã¹ã¯ãªãããæžããŠãããšãå®è¡æã«å€éšããæ å ±ãäžããããªãå Žé¢ããããããããŸããããäŸãã°ãåŠç察象ã®ãã¡ã€ã«ãã¹ãæå®ããããããã°ã©ã ã®æåãå€ãããã©ã°ãç«ãŠãã… ð€
ãããªæãã³ãã³ãã©ã€ã³ããåŒæ°ãåãåãæ©èœãå¿
èŠã«ãªããŸããPythonã«ã¯æšæºã§ sys.argv
ãšãããªã¹ãããããããã䜿ã£ãŠåŒæ°ãååŸã§ããŸãããåŒæ°ã®æ°ãå¢ãããããªãã·ã§ã³ïŒ--verbose
ã®ãããªãã€ãã³ã§å§ãŸãåŒæ°ïŒãæ±ã£ãããåãæå®ãããããã«ãã¡ãã»ãŒãžã衚瀺ããã…ãšãªããšãèªåã§ããŒã¹ïŒè§£æïŒããã®ã¯çµæ§å€§å€ã§ããðŠ
ããã§ç»å Žããã®ããPythonã®æšæºã©ã€ãã©ãªã§ãã argparse
ã§ãïŒ ð argparse
ã䜿ããšãã³ãã³ãã©ã€ã³åŒæ°ã®å®çŸ©ã解æããã«ãã¡ãã»ãŒãžã®èªåçæãªã©ãç°¡åãã€å®£èšçã«è¡ãããšãã§ããŸããããã«ããããŠãŒã¶ãŒãã¬ã³ããªãŒã§å
ç¢ãªã³ãã³ãã©ã€ã³ã€ã³ã¿ãŒãã§ãŒã¹ïŒCLIïŒãæã€ããã°ã©ã ãå¹ççã«éçºã§ããã®ã§ãã
ãã®ããã°ã§ã¯ãargparse
ã®åºæ¬çãªäœ¿ãæ¹ãããå°ãå¿çšçãªãã¯ããã¯ãŸã§ãå
·äœçãªã³ãŒãäŸã亀ããªãã詳ãã解説ããŠãããŸãããããäžç·ã« argparse
ã®äžçãæ¢æ€ããŸãããïŒðºïž
åºæ¬çãªäœ¿ãæ¹: åŒæ°ããŒãµãŒã®ã»ããã¢ãã
argparse
ã䜿ãããã®æåã®ã¹ãããã¯ãArgumentParser
ãªããžã§ã¯ããäœæããããšã§ãããããåŒæ°è§£æã®äžå¿çãªåœ¹å²ãæ
ããŸãã
import argparse
# ArgumentParserãªããžã§ã¯ããäœæ
# descriptionåŒæ°ã§ã--helpæã«è¡šç€ºãããããã°ã©ã ã®èª¬æãèšå®ã§ãã
parser = argparse.ArgumentParser(description='argparseã®åºæ¬çãªäœ¿ãæ¹ã瀺ããµã³ãã«ããã°ã©ã ')
print("ArgumentParserãªããžã§ã¯ããäœæãããŸããïŒâš")
# ããã§ã¯ãŸã åŒæ°ã®å®çŸ©ã解æã¯è¡ã£ãŠããŸãã
次ã«ãadd_argument()
ã¡ãœããã䜿ã£ãŠãåãä»ãããåŒæ°ãå®çŸ©ããŠãããŸããåŒæ°ã«ã¯å€§ããåããŠãäœçœ®åŒæ°ããšããªãã·ã§ã³åŒæ°ãã®2çš®é¡ããããŸãã
äœçœ®åŒæ° (Positional Arguments)
äœçœ®åŒæ°ã¯ãã³ãã³ãã©ã€ã³ã§æå®ããé çªãæå³ãæã€åŒæ°ã§ããéåžžãããã°ã©ã ã®å®è¡ã«å¿
é ãªæ
å ±ãæå®ããããã«äœ¿ãããŸããåŒæ°åããã€ãã³ (-
) ãããã«ãã€ãã³ (--
) ã§å§ããã«æå®ããŸãã
import argparse
parser = argparse.ArgumentParser(description='äœçœ®åŒæ°ã®äŸ')
# 'input_file' ãšããååã®äœçœ®åŒæ°ãè¿œå
# helpåŒæ°ã§ã--helpæã«è¡šç€ºãããåŒæ°ã®èª¬æãèšå®
parser.add_argument('input_file', help='åŠç察象ã®å
¥åãã¡ã€ã«ãã¹')
# 'output_file' ãšããååã®äœçœ®åŒæ°ãè¿œå
parser.add_argument('output_file', help='åŠççµæã®åºåãã¡ã€ã«ãã¹')
# parse_args()ã§å®éã«ã³ãã³ãã©ã€ã³åŒæ°ã解æ
# ããã§æå®ãããŠããªãåŒæ°ããããšãšã©ãŒã«ãªã
# args = parser.parse_args() # å®è¡ããã«ã¯ã³ã¡ã³ãã¢ãŠããå€ã
# print(f"å
¥åãã¡ã€ã«: {args.input_file}")
# print(f"åºåãã¡ã€ã«: {args.output_file}")
äžèšã®ã³ãŒãã `positional_example.py` ãšããŠä¿åããã³ãã³ãã©ã€ã³ããå®è¡ããŠã¿ãŸãããã
# ãã«ãã¡ãã»ãŒãžã衚瀺 (-h ãŸã㯠--help)
python positional_example.py -h
# å®è¡äŸ
python positional_example.py data.txt result.txt
# åŒæ°ã足ããªããšãšã©ãŒã«ãªã
python positional_example.py data.txt
-h
ãŸã㯠--help
ãä»ãããšãargparse
ãèªåçæãããã«ãã¡ãã»ãŒãžã衚瀺ãããŸãã䟿å©ã§ããïŒð
ãªãã·ã§ã³åŒæ° (Optional Arguments)
ãªãã·ã§ã³åŒæ°ã¯ããã€ãã³ (-
) ãŸãã¯ããã«ãã€ãã³ (--
) ã§å§ãŸãåŒæ°ã§ããéåžžãããã°ã©ã ã®æåãå¶åŸ¡ãããã©ã°ããå¿
é ã§ã¯ãªãèšå®å€ã®æå®ã«äœ¿ãããŸããçãåœ¢åŒ (-v
) ãšé·ãåœ¢åŒ (--verbose
) ã®äž¡æ¹ãå®çŸ©ããããšãå¯èœã§ãã
import argparse
parser = argparse.ArgumentParser(description='ãªãã·ã§ã³åŒæ°ã®äŸ')
# '-v' ãŸã㯠'--verbose' ãšãããªãã·ã§ã³åŒæ°ãè¿œå
# action='store_true' ã¯ããã®åŒæ°ãæå®ããããTrueãæ ŒçŽãããã©ã°ãšããŠæ±ã
parser.add_argument('-v', '--verbose', action='store_true', help='詳现ãªãã°ãåºåãã')
# '--level' ãšãããªãã·ã§ã³åŒæ°ãè¿œå
# type=int ã§åŒæ°ã®åãæŽæ°ã«æå®
# default=1 ã§ããã©ã«ãå€ãèšå®
parser.add_argument('--level', type=int, default=1, help='åŠçã¬ãã«ãæå® (ããã©ã«ã: 1)')
# args = parser.parse_args() # å®è¡ããã«ã¯ã³ã¡ã³ãã¢ãŠããå€ã
# if args.verbose:
# print("詳现ã¢ãŒããæå¹ã§ãã")
# else:
# print("éåžžã¢ãŒãã§ãã")
# print(f"åŠçã¬ãã«: {args.level}")
äžèšã®ã³ãŒãã `optional_example.py` ãšããŠä¿åããå®è¡ããŠã¿ãŸãããã
# ãã«ãã¡ãã»ãŒãžã衚瀺
python optional_example.py -h
# ãªãã·ã§ã³ãæå®ããã«å®è¡ (ããã©ã«ãå€ã䜿ããã)
python optional_example.py
# ãªãã·ã§ã³ãæå®ããŠå®è¡
python optional_example.py --verbose --level 3
# ãŸãã¯çã圢åŒã§
python optional_example.py -v --level 3
åŒæ°ã®è§£æãšå€ãžã®ã¢ã¯ã»ã¹
add_argument()
ã§åŒæ°ãå®çŸ©ããåŸãparse_args()
ã¡ãœãããåŒã³åºãããšã§ãå®éã«ã³ãã³ãã©ã€ã³ããäžããããåŒæ° (sys.argv
) ã解æãããŸãã
parse_args()
ã¯ã解æçµæãå±æ§ãšããŠæ〠`Namespace` ãªããžã§ã¯ããè¿ããŸããåå±æ§ã®ååã¯ãadd_argument()
ã§æå®ããåŒæ°åïŒéåžžã¯é·ã圢åŒã®ååããå
é ã®ãã€ãã³ãé€ãããã®ããŸãã¯äœçœ®åŒæ°ã®ååïŒã«å¯Ÿå¿ããŸãã
import argparse
parser = argparse.ArgumentParser(description='åŒæ°è§£æã®äŸ')
parser.add_argument('filename', help='ãã¡ã€ã«å')
parser.add_argument('-n', '--lines', type=int, default=10, help='衚瀺ããè¡æ°')
parser.add_argument('--encoding', default='utf-8', help='ãã¡ã€ã«ã®ãšã³ã³ãŒãã£ã³ã°')
# ã³ãã³ãã©ã€ã³åŒæ°ã解æ
args = parser.parse_args()
# 解æçµæã«ã¢ã¯ã»ã¹
print(f"ãã¡ã€ã«å: {args.filename}")
print(f"衚瀺è¡æ°: {args.lines}")
print(f"ãšã³ã³ãŒãã£ã³ã°: {args.encoding}")
# äŸ: ãã¡ã€ã«ãèªã¿èŸŒãã§æå®è¡æ°ã衚瀺ããåŠç (å®éã®ãã¡ã€ã«åŠçã¯çç¥)
print(f"\n--- {args.filename} ã®æåã® {args.lines} è¡ ({args.encoding}) ---")
# with open(args.filename, 'r', encoding=args.encoding) as f:
# for i, line in enumerate(f):
# if i >= args.lines:
# break
# print(line.strip())
ããã `parse_example.py` ãšããŠä¿åããå®è¡ããŸãã
# å®è¡äŸ
python parse_example.py my_document.txt --lines 5 --encoding cp932
# ãªãã·ã§ã³ã®é çªã¯åããªã (äœçœ®åŒæ°ãé€ã)
python parse_example.py my_document.txt --encoding cp932 -n 5
# ããã©ã«ãå€ã䜿ãå Žå
python parse_example.py another_file.log
ãã®ããã«ãargparse
ã䜿ãã°ãåºæ¬çãªã³ãã³ãã©ã€ã³åŒæ°ã®åãåãã¯éåžžã«ç°¡åã«è¡ããŸããïŒð
åŒæ°ã®è©³çŽ°èšå®: åãã¢ã¯ã·ã§ã³ãå¶çŽãªã©
add_argument()
ã¡ãœããã«ã¯ãåŒæ°ã®æåã现ããå¶åŸ¡ããããã®æ§ã
ãªãªãã·ã§ã³ãçšæãããŠããŸããããã§ã¯äž»èŠãªãã®ãããã€ãèŠãŠãããŸãããã
type: åŒæ°ã®åãæå®ãã
ã³ãã³ãã©ã€ã³åŒæ°ã¯éåžžæååãšããŠæž¡ãããŸãããtype
ãªãã·ã§ã³ã«é¢æ°ïŒãŸãã¯åŒã³åºãå¯èœãªãªããžã§ã¯ãïŒãæå®ããããšã§ãå€ãç¹å®ã®åã«å€æã§ããŸãããã䜿ãããã®ã¯ int
, float
, str
ãªã©ã§ãã
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--port', type=int, help='æ¥ç¶å
ã®ããŒãçªå· (æŽæ°)')
parser.add_argument('--threshold', type=float, default=0.5, help='éŸå€ (æµ®åå°æ°ç¹æ°)')
args = parser.parse_args()
print(f"ããŒãçªå·: {args.port} (å: {type(args.port)})")
print(f"éŸå€: {args.threshold} (å: {type(args.threshold)})")
ããäžæ£ãªå€ïŒäŸ: --port abc
ïŒãäžããããå Žåãargparse
ã¯èªåçã«ãšã©ãŒã¡ãã»ãŒãžã衚瀺ããŠçµäºããŸããð¡ïž
ããã«ãèªåã§å®çŸ©ããé¢æ°ã type
ã«æå®ããããšãå¯èœã§ããäŸãã°ãæ£ã®æŽæ°ã®ã¿ãåãä»ããåãã§ãã«ãŒãäœãããšãã§ããŸãã
import argparse
def positive_int(value):
"""æ£ã®æŽæ°ããã§ãã¯ããé¢æ°"""
try:
ivalue = int(value)
except ValueError:
raise argparse.ArgumentTypeError(f"'{value}' ã¯æŽæ°ã§ã¯ãããŸããã")
if ivalue <= 0:
raise argparse.ArgumentTypeError(f"'{value}' ã¯æ£ã®æŽæ°ã§ã¯ãããŸããã")
return ivalue
parser = argparse.ArgumentParser()
parser.add_argument('--count', type=positive_int, help='åŠçåæ° (æ£ã®æŽæ°)')
args = parser.parse_args()
print(f"åŠçåæ°: {args.count}")
action: åŒæ°ãæå®ããããšãã®åäœ
action
ãªãã·ã§ã³ã¯ãã³ãã³ãã©ã€ã³ã§åŒæ°ãæå®ããããšãã«ãargparse
ãã©ã®ãããªåäœããããã決å®ããŸããããã©ã«ã㯠'store'
ã§ãåŒæ°ã®å€ããã®ãŸãŸæ ŒçŽããŸãã
ãã䜿ããã action
ã®å€ãããã€ã玹ä»ããŸãã
action ã®å€ |
説æ | äŸ |
---|---|---|
'store' |
åŒæ°ã®å€ãå±æ§ã«æ ŒçŽããŸãïŒããã©ã«ãïŒãtype ã§æå®ãããåã«å€æãããŸãã |
parser.add_argument('--name', action='store') |
'store_const' |
åŒæ°ãæå®ãããå Žåã«ãconst ãªãã·ã§ã³ã§æå®ãããåºå®å€ãæ ŒçŽããŸãã |
parser.add_argument('--mode', action='store_const', const='expert') |
'store_true' |
åŒæ°ãæå®ãããå Žåã« True ãæ ŒçŽããŸãããã©ã°ãšããŠäŸ¿å©ã§ããæå®ãããªããã° False (ããã©ã«ã)ã |
parser.add_argument('--debug', action='store_true') |
'store_false' |
åŒæ°ãæå®ãããå Žåã« False ãæ ŒçŽããŸããããã©ã«ãã§æå¹ãªæ©èœãç¡å¹ã«ãããã©ã°ã«äŸ¿å©ã§ããæå®ãããªããã° True (ããã©ã«ã)ã |
parser.add_argument('--no-cache', action='store_false', dest='use_cache') |
'append' |
åŒæ°ãæå®ããããã³ã«ããã®å€ããªã¹ãã«è¿œå ããŸããåããªãã·ã§ã³ãè€æ°åæå®ããå Žåã«äœ¿ããŸãã | parser.add_argument('-I', '--include', action='append', dest='include_dirs') |
'append_const' |
åŒæ°ãæå®ããããã³ã«ãconst ã§æå®ãããåºå®å€ããªã¹ãã«è¿œå ããŸãã |
parser.add_argument('--verbose', action='append_const', const=1, dest='verbosity') |
'count' |
åŒæ°ãæå®ãããåæ°ãã«ãŠã³ãããŸããåé·ã¬ãã«ã®æå® (-vvv ) ãªã©ã«äœ¿ããŸãã |
parser.add_argument('-v', '--verbose', action='count', default=0) |
'help' |
ãã«ãã¡ãã»ãŒãžã衚瀺ããŠããã°ã©ã ãçµäºããŸãã-h , --help ã«èªåã§è¿œå ãããŸãã |
(éåžžã¯èªå) |
'version' |
version ãªãã·ã§ã³ã§æå®ãããããŒãžã§ã³æ
å ±ã衚瀺ããŠçµäºããŸãã |
parser.add_argument('--version', action='version', version='%(prog)s 2.0') |
import argparse
parser = argparse.ArgumentParser(prog='action_example', description='æ§ã
ãªactionã®äŸ')
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
# store_true / store_false
parser.add_argument('--enable-feature', action='store_true', help='æ©èœãæå¹å')
parser.add_argument('--disable-log', action='store_false', dest='logging_enabled', help='ãã°åºåãç¡å¹å')
parser.set_defaults(logging_enabled=True) # store_falseã®ããã©ã«ãã¯Trueã«ããã®ãäžè¬ç
# append
parser.add_argument('-t', '--tag', action='append', help='ã¿ã°ãè€æ°æå®å¯èœ')
# count
parser.add_argument('-v', action='count', default=0, help='åé·ã¬ãã«ãäžãã (äŸ: -vv)')
args = parser.parse_args()
print(f"æ©èœæå¹ãã©ã°: {args.enable_feature}")
print(f"ãã°åºåæå¹ãã©ã°: {args.logging_enabled}")
print(f"æå®ãããã¿ã°: {args.tag}") # ãªã¹ãã«ãªã
print(f"åé·ã¬ãã«: {args.v}")
å®è¡äŸ:
python action_example.py --enable-feature --disable-log -t web -t api -vv
# åºå:
# æ©èœæå¹ãã©ã°: True
# ãã°åºåæå¹ãã©ã°: False
# æå®ãããã¿ã°: ['web', 'api']
# åé·ã¬ãã«: 2
python action_example.py --version
# åºå:
# action_example 1.0
default: ããã©ã«ãå€
åŒæ°ãã³ãã³ãã©ã€ã³ã§æå®ãããªãã£ãå Žåã«èšå®ãããå€ã default
ã§æå®ã§ããŸããããã©ã«ã㯠None
ã§ãã
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--timeout', type=int, default=30, help='ã¿ã€ã ã¢ãŠãç§ (ããã©ã«ã: 30)')
args = parser.parse_args()
print(f"ã¿ã€ã ã¢ãŠã: {args.timeout}")
required: å¿ é ã®åŒæ°
éåžžããªãã·ã§ã³åŒæ°ã¯çç¥å¯èœã§ãããrequired=True
ãæå®ãããšããã®ãªãã·ã§ã³åŒæ°ãå¿
é ã«ãªããŸããæå®ãããªãå Žåã¯ãšã©ãŒãšãªããŸãã
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--config', required=True, help='èšå®ãã¡ã€ã«ã®ãã¹ (å¿
é )')
args = parser.parse_args()
print(f"èšå®ãã¡ã€ã«: {args.config}")
泚æç¹ãšããŠãäœçœ®åŒæ°ã¯ããã©ã«ãã§å¿
é ã§ããäœçœ®åŒæ°ãçç¥å¯èœã«ãããå Žåã¯ãåŸè¿°ãã nargs
ã䜿ããŸãã
choices: éžæè¢ã®å¶é
åŒæ°ã«æå®ã§ããå€ãç¹å®ã®ãªã¹ãã«å¶éãããå Žå㯠choices
ã䜿ããŸãããªã¹ãã«å«ãŸããªãå€ãæå®ããããšãšã©ãŒã«ãªããŸãã
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--mode', choices=['read', 'write', 'append'], default='read', help='åäœã¢ãŒããéžæ')
args = parser.parse_args()
print(f"åäœã¢ãŒã: {args.mode}")
å®è¡äŸ:
python choice_example.py --mode write # OK
python choice_example.py --mode delete # ãšã©ãŒ
help: ãã«ãã¡ãã»ãŒãž
æ¢ã«äœåºŠãç»å ŽããŠããŸãããhelp
ãªãã·ã§ã³ã§ã-h
ã --help
ãæå®ããããšãã«è¡šç€ºãããåŒæ°ã®èª¬ææãèšå®ã§ããŸããåããããããã«ãã¡ãã»ãŒãžãæžãããšã¯ãCLIããŒã«ã®äœ¿ããããã«ãšã£ãŠéåžžã«éèŠã§ããâïž
ArgumentParser
ã®ã³ã³ã¹ãã©ã¯ã¿ã§ description
(ããã°ã©ã å
šäœã®èª¬æ) ã epilog
(ãã«ãã¡ãã»ãŒãžã®åŸã«è¿œå ãããããã¹ã) ãæå®ããããšãã§ããŸãã
import argparse
parser = argparse.ArgumentParser(
description='ãã¡ã€ã«ã®æž©åºŠãå€æããããã°ã©ã ã',
epilog='äŸ: python temp_converter.py 25 --to-fahrenheit'
)
parser.add_argument('temperature', type=float, help='å€æãã枩床ã®å€')
parser.add_argument('--to-fahrenheit', action='store_true', help='ææ°ãè¯æ°ã«å€æ')
parser.add_argument('--to-celsius', action='store_true', help='è¯æ°ãææ°ã«å€æ')
args = parser.parse_args()
# (å®éã®å€æåŠçã¯çç¥)
if args.to_fahrenheit:
print(f"{args.temperature}°C 㯠...°F ã§ãã")
elif args.to_celsius:
print(f"{args.temperature}°F 㯠...°C ã§ãã")
else:
print("å€æã¢ãŒã (--to-fahrenheit ãŸã㯠--to-celsius) ãæå®ããŠãã ããã")
ãã«ãã¡ãã»ãŒãžã確èªããŠã¿ãŸããã:
python temp_converter.py -h
metavar: ãã«ãã¡ãã»ãŒãžã§ã®åŒæ°å€ã®è¡šç€ºå
ãã«ãã¡ãã»ãŒãžäžã§ãåŒæ°ãåãå€ã®ãã¬ãŒã¹ãã«ããŒïŒäŸ: [-n NAME]
ã® NAME
éšåïŒãã«ã¹ã¿ãã€ãºãããå Žåã« metavar
ã䜿ããŸããããã©ã«ãã§ã¯åŒæ°åã倧æåã«ãªã£ããã®ã䜿ãããŸãã
import argparse
parser = argparse.ArgumentParser()
# ãã«ã㧠[-i INPUT_PATH] ã®ããã«è¡šç€ºããã
parser.add_argument('-i', '--input', metavar='INPUT_PATH', help='å
¥åãã¡ã€ã«ã®ãã¹')
# äœçœ®åŒæ°ã§ã䜿ãã
parser.add_argument('user_id', metavar='USER_ID', help='ãŠãŒã¶ãŒID')
args = parser.parse_args()
dest: æ ŒçŽå ã®å±æ§å
parse_args()
ãè¿ã `Namespace` ãªããžã§ã¯ãã«æ ŒçŽãããéã®å±æ§åãæ瀺çã«æå®ãããå Žåã« dest
ã䜿ããŸããç¹ã«ãaction='store_false'
ãããã€ãã³ãå«ããªãã·ã§ã³å (--long-option-name
) ã䜿ãå Žåã«äŸ¿å©ã§ãã
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--no-verify', action='store_false', dest='verify_ssl', help='SSLæ€èšŒãç¡å¹å')
parser.set_defaults(verify_ssl=True) # ããã©ã«ãã¯æ€èšŒãã
parser.add_argument('--output-dir', dest='output_directory', default='/tmp', help='åºåãã£ã¬ã¯ããª')
args = parser.parse_args()
if args.verify_ssl:
print("SSLæ€èšŒãè¡ããŸãã")
else:
print("SSLæ€èšŒãã¹ãããããŸãã")
print(f"åºåãã£ã¬ã¯ããª: {args.output_directory}")
ãããã®ãªãã·ã§ã³ãçµã¿åãããããšã§ãéåžžã«æè»ãã€å ç¢ãªã³ãã³ãã©ã€ã³åŒæ°ã®åŠçãå¯èœã«ãªããŸããðª
ããé«åºŠãªæ©èœ ð
argparse
ã«ã¯ãåºæ¬çãªåŒæ°å®çŸ©ä»¥å€ã«ããããè€éãªCLIãæ§ç¯ããããã®æ©èœãåãã£ãŠããŸãã
nargs: åŒæ°ã®æ°
nargs
ãªãã·ã§ã³ã䜿ããšãäžã€ã®åŒæ°åã§è€æ°ã®å€ãåãåãããšãã§ããŸãã
nargs ã®å€ |
説æ | æ ŒçŽãããå€ |
---|---|---|
N (æŽæ°) |
ã¡ããã© N åã®åŒæ°ãæ¶è²»ããŸãã | N åã®èŠçŽ ãæã€ãªã¹ã |
'?' |
0åãŸãã¯1åã®åŒæ°ãæ¶è²»ããŸããæå®ãããªãã£ãå Žå㯠default å€ãæå®ãããå Žåã¯ãã®å€ã |
åäžã®å€ or default å€ |
'*' |
0å以äžã®åŒæ°ãæ¶è²»ããŸãã | èŠçŽ ã0å以äžã®ãªã¹ã |
'+' |
1å以äžã®åŒæ°ãæ¶è²»ããŸããæäœ1ã€ã¯å¿ èŠã§ãã | èŠçŽ ã1å以äžã®ãªã¹ã |
argparse.REMAINDER |
æ®ãã®ãã¹ãŠã®ã³ãã³ãã©ã€ã³åŒæ°ãæ¶è²»ããŸãã | æ®ãã®åŒæ°ã®ãªã¹ã |
import argparse
parser = argparse.ArgumentParser(description='nargs ã®äŸ')
# ã¡ããã©2ã€ã®åº§æšãåãåã
parser.add_argument('--pos', nargs=2, type=float, metavar=('X', 'Y'), help='åº§æš (X Y)')
# 0å以äžã®å
¥åãã¡ã€ã«ãåãåã
parser.add_argument('--input-files', nargs='*', default=[], help='å
¥åãã¡ã€ã« (è€æ°æå®å¯)')
# 1å以äžã®ã¿ã°ãåãåã
parser.add_argument('--tags', nargs='+', required=True, help='ã¿ã° (æäœ1ã€å¿
é )')
# ãªãã·ã§ã³åŒæ°ã§ '?' ã䜿ãäŸ (ããã©ã«ãå€ä»ã)
parser.add_argument('--config', nargs='?', const='config.ini', default=None,
help='èšå®ãã¡ã€ã«ãã¹ (æå®ãªã or ãã¹æå®)')
# constã¯åŒæ°åã®ã¿æå®ãããå Žåã®å€
# äœçœ®åŒæ°ã§ '?' ã䜿ãäŸ
# parser.add_argument('output', nargs='?', default='output.txt', help='åºåãã¡ã€ã«å (çç¥å¯)')
args = parser.parse_args()
print(f"座æš: {args.pos}")
print(f"å
¥åãã¡ã€ã«: {args.input_files}")
print(f"ã¿ã°: {args.tags}")
print(f"èšå®ãã¡ã€ã«: {args.config}")
# print(f"åºåãã¡ã€ã«: {args.output}")
å®è¡äŸ:
# æ£åžžãªäŸ
python nargs_example.py --pos 10.5 20.0 --input-files a.txt b.txt --tags web db api --config my_conf.yaml
# åºå:
# 座æš: [10.5, 20.0]
# å
¥åãã¡ã€ã«: ['a.txt', 'b.txt']
# ã¿ã°: ['web', 'db', 'api']
# èšå®ãã¡ã€ã«: my_conf.yaml
# --tags ããªããšãšã©ãŒ
python nargs_example.py --pos 1 2
# --input-files ã¯ãªããŠãOK
python nargs_example.py --pos 1 2 --tags test
# --config ã®ã¿æå® (constã®å€ã䜿ããã)
python nargs_example.py --pos 1 2 --tags test --config
# åºå:
# èšå®ãã¡ã€ã«: config.ini
# --config ãæå®ããªã (defaultå€ã䜿ããã)
python nargs_example.py --pos 1 2 --tags test
# åºå:
# èšå®ãã¡ã€ã«: None
åŒæ°ã°ã«ãŒã (Argument Groups)
é¢é£ããåŒæ°ããã«ãã¡ãã»ãŒãžå
ã§ã°ã«ãŒãåããŠè¡šç€ºãããå Žåãadd_argument_group()
ã䜿ããŸããããã«ããããã«ãã¡ãã»ãŒãžãæŽçãããèŠããããªããŸãã
import argparse
parser = argparse.ArgumentParser(description='åŒæ°ã°ã«ãŒãã®äŸ', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# ã°ã«ãŒã1: å
¥åºåèšå®
group_io = parser.add_argument_group('Input/Output options')
group_io.add_argument('--input', required=True, help='å
¥åãã¡ã€ã«')
group_io.add_argument('--output', default='out.dat', help='åºåãã¡ã€ã«')
# ã°ã«ãŒã2: åŠçèšå®
group_proc = parser.add_argument_group('Processing options')
group_proc.add_argument('--iterations', type=int, default=10, help='å埩åæ°')
group_proc.add_argument('--tolerance', type=float, default=1e-5, help='蚱容誀差')
args = parser.parse_args()
print("åŒæ°ãã°ã«ãŒãåãããŠãã«ãã衚瀺ãããŸãã")
print(f"å
¥å: {args.input}")
print(f"åºå: {args.output}")
print(f"å埩åæ°: {args.iterations}")
print(f"蚱容誀差: {args.tolerance}")
# formatter_class=argparse.ArgumentDefaultsHelpFormatter ãæå®ãããšã
# ãã«ãã¡ãã»ãŒãžã«ããã©ã«ãå€ã衚瀺ãããããã«ãªããŸãã
ãã«ãã¡ãã»ãŒãž (python group_example.py -h
) ãèŠããšãåŒæ°ãã°ã«ãŒãããšã«è¡šç€ºãããŠããã®ãããããŸãã
çžäºæä»çãªåŒæ° (Mutually Exclusive Arguments)
åæã«æå®ã§ããªãåŒæ°ã®çµã¿åããïŒäŸ: --verbose
ãš --quiet
ïŒãå®çŸ©ãããå Žåã¯ãadd_mutually_exclusive_group()
ã䜿ããŸãã
import argparse
parser = argparse.ArgumentParser(description='çžäºæä»çãªåŒæ°ã®äŸ')
# çžäºæä»çãªã°ã«ãŒããäœæ
group = parser.add_mutually_exclusive_group()
group.add_argument('--verbose', action='store_true', help='詳现ãªãã°ãåºå')
group.add_argument('--quiet', action='store_true', help='ãã°åºåãæå¶')
# ãã®ã°ã«ãŒãå
ã§ã¯ required=True ã¯æå®ã§ããªãããšãå€ã
# ã°ã«ãŒãå
šäœã®æå®ãå¿
é ã«ãããå Žåã¯ãã°ã«ãŒãäœææã« required=True ãæå®
# å¥ã®åŒæ°ãè¿œå å¯èœ
parser.add_argument('filename', help='ãã¡ã€ã«å')
args = parser.parse_args()
if args.verbose:
print("詳现ã¢ãŒã")
elif args.quiet:
print("éå¯ã¢ãŒã")
else:
print("éåžžã¢ãŒã") # ã©ã¡ããæå®ãããªãã£ãå Žå
print(f"ãã¡ã€ã«å: {args.filename}")
å®è¡äŸ:
python exclusive_example.py data.log --verbose # OK
python exclusive_example.py data.log --quiet # OK
python exclusive_example.py data.log # OK (ã©ã¡ããæå®ããªã)
python exclusive_example.py data.log --verbose --quiet # ãšã©ãŒ
ãµãã³ãã³ã (Sub-commands / Sub-parsers)
git
ã³ãã³ã (git commit
, git push
ãªã©) ã®ããã«ãã¡ã€ã³ã³ãã³ãã«ç¶ããŠãµãã³ãã³ããæå®ãããµãã³ãã³ãããšã«ç°ãªãåŒæ°ãåãä»ãããå ŽåããããŸãããã㯠add_subparsers()
ã䜿ã£ãŠå®çŸã§ããŸãã
import argparse
# ã¡ã€ã³ã®ããŒãµãŒ
parser = argparse.ArgumentParser(description='ãµãã³ãã³ããæã€ããã°ã©ã ã®äŸ')
# ãµãããŒãµãŒãè¿œå ããæºå
# dest='command' ã§ãã©ã®ãµãã³ãã³ããéžã°ããããæ ŒçŽããå±æ§åãæå®
# required=True ã§ãµãã³ãã³ãã®æå®ãå¿
é ã«ãã
subparsers = parser.add_subparsers(dest='command', required=True, help='å©çšå¯èœãªãµãã³ãã³ã')
# ãµãã³ãã³ã 'commit' ã®ããŒãµãŒ
parser_commit = subparsers.add_parser('commit', help='å€æŽãã³ããããã')
parser_commit.add_argument('-m', '--message', required=True, help='ã³ãããã¡ãã»ãŒãž')
parser_commit.add_argument('--amend', action='store_true', help='çŽåã®ã³ããããä¿®æ£')
# ãµãã³ãã³ã 'push' ã®ããŒãµãŒ
parser_push = subparsers.add_parser('push', help='ãªã¢ãŒããªããžããªã«å€æŽãããã·ã¥ãã')
parser_push.add_argument('remote', default='origin', nargs='?', help='ãªã¢ãŒãå (ããã©ã«ã: origin)')
parser_push.add_argument('branch', nargs='?', help='ãã©ã³ãå (çç¥æã¯çŸåšã®ãã©ã³ã)')
parser_push.add_argument('--force', action='store_true', help='匷å¶ããã·ã¥')
# åŒæ°ã解æ
args = parser.parse_args()
# ã©ã®ãµãã³ãã³ããå®è¡ããããã«å¿ããŠåŠçãåå²
if args.command == 'commit':
print("Commit ãµãã³ãã³ããå®è¡ãããŸããã")
print(f" ã¡ãã»ãŒãž: {args.message}")
if args.amend:
print(" --amend ãã©ã°ãæå®ãããŸããã")
elif args.command == 'push':
print("Push ãµãã³ãã³ããå®è¡ãããŸããã")
print(f" ãªã¢ãŒã: {args.remote}")
print(f" ãã©ã³ã: {args.branch if args.branch else '(çŸåšã®ãã©ã³ã)'}")
if args.force:
print(" --force ãã©ã°ãæå®ãããŸããã")
else:
# éåžž required=True ãªã®ã§ããã«ã¯æ¥ãªãã¯ã
parser.print_help()
å®è¡äŸ:
# ãã«ãã衚瀺 (ãµãã³ãã³ãäžèŠ§ã衚瀺ããã)
python subcommand_example.py -h
# commitãµãã³ãã³ãã®ãã«ãã衚瀺
python subcommand_example.py commit -h
# commitãµãã³ãã³ããå®è¡
python subcommand_example.py commit -m "æåã®ã³ããã"
# pushãµãã³ãã³ããå®è¡
python subcommand_example.py push upstream main --force
ãµãã³ãã³ãã䜿ãããšã§ãå€æ©èœãªCLIããŒã«ãæŽçããã圢ã§æäŸã§ããŸãã倧èŠæš¡ãªã¢ããªã±ãŒã·ã§ã³ã§ã¯éåžžã«åœ¹ç«ã€æ©èœã§ããð ïž
ãã¡ã€ã«ããã®åŒæ°èªã¿èŸŒã¿ (fromfile_prefix_chars)
ã³ãã³ãã©ã€ã³åŒæ°ãéåžžã«é·ããªãå ŽåãåŒæ°ããã¡ã€ã«ã«èšè¿°ããŠããããããèªã¿èŸŒãŸããããšãã§ããŸããArgumentParser
ã®ã³ã³ã¹ãã©ã¯ã¿ã§ fromfile_prefix_chars
ã«ãã¬ãã£ãã¯ã¹æåïŒäŸ: '@'
ïŒãæå®ããŸãã
import argparse
# args.txt ãšãããã¡ã€ã«ãäœæãã以äžã®ããã«èšè¿°ããŠãã:
# --input
# data/input_file.csv
# --output
# results/output.csv
# --iterations
# 100
# --verbose
parser = argparse.ArgumentParser(
description='ãã¡ã€ã«ããåŒæ°ãèªã¿èŸŒãäŸ',
fromfile_prefix_chars='@' # '@'ã§å§ãŸãåŒæ°ããã¡ã€ã«ãã¹ãšããŠè§£é
)
parser.add_argument('--input', required=True, help='å
¥åãã¡ã€ã«')
parser.add_argument('--output', required=True, help='åºåãã¡ã€ã«')
parser.add_argument('--iterations', type=int, default=10, help='å埩åæ°')
parser.add_argument('--verbose', action='store_true', help='詳现åºå')
args = parser.parse_args()
print(f"å
¥åãã¡ã€ã«: {args.input}")
print(f"åºåãã¡ã€ã«: {args.output}")
print(f"å埩åæ°: {args.iterations}")
print(f"詳现åºå: {args.verbose}")
å®è¡äŸ:
# args.txt ãã¡ã€ã«ã®å
容ãåŒæ°ãšããŠèªã¿èŸŒã
python fromfile_example.py @args.txt
# ãã¡ã€ã«ããã®èªã¿èŸŒã¿ãšéåžžã®åŒæ°ãçµã¿åãããããšãå¯èœ
# python fromfile_example.py @args.txt --iterations 50 # ãã¡ã€ã«ã®å€ãäžæžãããã
ã«ã¹ã¿ã ã¢ã¯ã·ã§ã³ (Custom Actions)
action
ã«ã¯ãargparse.Action
ãç¶æ¿ããã«ã¹ã¿ã ã¯ã©ã¹ãæå®ããããšãã§ããŸããããã«ãããåŒæ°è§£ææã«ç¬èªã®ããžãã¯ãå®è¡ã§ããŸããäŸãã°ãç¹å®ã®åœ¢åŒã®æååãããŒã¹ããŠãªããžã§ã¯ãã«å€æãããªã©ãè€éãªåŠçãå¯èœã§ãã
import argparse
import os
class PathAction(argparse.Action):
"""ãã¹ãååšããããã§ãã¯ãã絶察ãã¹ã«å€æããã«ã¹ã¿ã ã¢ã¯ã·ã§ã³"""
def __call__(self, parser, namespace, values, option_string=None):
path = os.path.abspath(os.path.expanduser(values))
if not os.path.exists(path):
parser.error(f"æå®ããããã¹ãèŠã€ãããŸãã: {values}")
# argparse.ArgumentTypeError ã raise ããæ¹æ³ããã
# raise argparse.ArgumentTypeError(f"æå®ããããã¹ãèŠã€ãããŸãã: {values}")
setattr(namespace, self.dest, path)
parser = argparse.ArgumentParser(description='ã«ã¹ã¿ã ã¢ã¯ã·ã§ã³ã®äŸ')
parser.add_argument('--config-dir', action=PathAction, required=True, help='èšå®ãã£ã¬ã¯ã㪠(ååšç¢ºèªãšçµ¶å¯Ÿãã¹å)')
args = parser.parse_args()
print(f"èšå®ãã£ã¬ã¯ã㪠(絶察ãã¹): {args.config_dir}")
ãã®ã«ã¹ã¿ã ã¢ã¯ã·ã§ã³ã¯ãæå®ããããã¹ãååšããããã§ãã¯ããååšããã°çµ¶å¯Ÿãã¹ã«å€æã㊠`Namespace` ãªããžã§ã¯ãã«æ ŒçŽããŸãã
ãšã©ãŒåŠçãšã¡ãã»ãŒãžã®ã«ã¹ã¿ãã€ãº
argparse
ã¯äžæ£ãªåŒæ°ãäžããããå Žåã«èªåã§ãšã©ãŒã¡ãã»ãŒãžã衚瀺ããŠçµäºããŸãããArgumentParser
ã® error()
ã¡ãœããããªãŒããŒã©ã€ãããããparse_args()
ã try...except SystemExit
ã§å²ãããšã§ããšã©ãŒåŠçãã«ã¹ã¿ãã€ãºããããšãå¯èœã§ãã
ãŸãããã«ãã¡ãã»ãŒãžããšã©ãŒã¡ãã»ãŒãžã®äžéšã¯ãPythonã® `gettext` ã¢ãžã¥ãŒã«ã䜿ã£ãŠåœéåïŒå€èšèªå¯Ÿå¿ïŒããããšãã§ããŸãã
å®è·µçãªäŸãšãã¹ããã©ã¯ãã£ã¹ ð¡
å®è·µäŸ: ç°¡åãªãã¡ã€ã«ã³ããŒã³ãã³ã
ãããŸã§ã«åŠãã ããšã掻ãããŠãç°¡åãªãã¡ã€ã«ã³ããŒã³ãã³ããäœæããŠã¿ãŸãããã
import argparse
import shutil
import os
import sys
def main():
parser = argparse.ArgumentParser(
description='ãã¡ã€ã«ãã³ããŒããã·ã³ãã«ãªã³ãã³ã',
formatter_class=argparse.RawDescriptionHelpFormatter, # helpã®æžåŒãä¿ã€
epilog="""
䜿çšäŸ:
%(prog)s source.txt destination.txt # åºæ¬çãªã³ããŒ
%(prog)s -v source.txt destination_dir/ # 詳现衚瀺ã§ãã£ã¬ã¯ããªã«ã³ããŒ
%(prog)s -i *.log backup/ # ã¯ã€ã«ãã«ãŒãã¯ã·ã§ã«ãå±éããåæ
"""
)
# äœçœ®åŒæ°
parser.add_argument('source', help='ã³ããŒå
ã®ãã¡ã€ã«ãŸãã¯ãã£ã¬ã¯ããª')
parser.add_argument('destination', help='ã³ããŒå
ã®ãã¡ã€ã«ãŸãã¯ãã£ã¬ã¯ããª')
# ãªãã·ã§ã³åŒæ°
parser.add_argument('-v', '--verbose', action='store_true', help='詳现ãªæ
å ±ã衚瀺ãã')
parser.add_argument('-i', '--interactive', action='store_true', help='äžæžãåã«ç¢ºèªãã')
parser.add_argument('-f', '--force', action='store_true', help='確èªãªãã§äžæžããã')
# çžäºæä»çãªã°ã«ãŒã (--interactive ãš --force)
group = parser.add_mutually_exclusive_group()
group.add_argument('--backup', action='store_true', help='äžæžãããå Žåã«ããã¯ã¢ãããäœæãã (.bak)')
args = parser.parse_args()
# --- å
¥åãã§ã㯠---
if not os.path.exists(args.source):
parser.error(f"ãšã©ãŒ: ã³ããŒå
ãèŠã€ãããŸãã: {args.source}")
if args.interactive and args.force:
# æ¬æ¥ add_mutually_exclusive_group ã§é²ãããã念ã®ãã
parser.error("ãšã©ãŒ: --interactive ãš --force ã¯åæã«æå®ã§ããŸããã")
# --- ã³ããŒåŠç ---
try:
# ã³ããŒå
ããã£ã¬ã¯ããªã®å Žåããã¡ã€ã«åãç¶æ
dest_path = args.destination
if os.path.isdir(args.destination):
dest_path = os.path.join(args.destination, os.path.basename(args.source))
# äžæžããã§ãã¯
if os.path.exists(dest_path):
if args.force:
if args.verbose:
print(f"'{dest_path}' ã匷å¶çã«äžæžãããŸãã")
elif args.interactive:
response = input(f"'{dest_path}' ã¯æ¢ã«ååšããŸããäžæžãããŸãã? (y/N): ").lower()
if response != 'y':
print("ã³ããŒããã£ã³ã»ã«ããŸããã")
sys.exit(0)
elif args.backup:
backup_path = dest_path + ".bak"
if args.verbose:
print(f"'{dest_path}' ã '{backup_path}' ã«ããã¯ã¢ããããŸãã")
shutil.move(dest_path, backup_path)
else:
parser.error(f"ãšã©ãŒ: ã³ããŒå
ãæ¢ã«ååšããŸã: {dest_path}\n"
"äžæžãããã«ã¯ --force ãã確èªããã«ã¯ --interactive ãã"
"ããã¯ã¢ããããã«ã¯ --backup ãæå®ããŠãã ããã")
# ãã¡ã€ã«ã³ããŒã®å®è¡
if args.verbose:
print(f"'{args.source}' ã '{dest_path}' ã«ã³ããŒããŠããŸã...")
shutil.copy2(args.source, dest_path) # copy2ã¯ã¡ã¿ããŒã¿ãã³ããŒãã
if args.verbose:
print("ã³ããŒãå®äºããŸãããð")
except Exception as e:
parser.error(f"ã³ããŒäžã«ãšã©ãŒãçºçããŸãã: {e}")
if __name__ == '__main__':
main()
ãã®äŸã§ã¯ãäœçœ®åŒæ°ããªãã·ã§ã³åŒæ°ãaction='store_true'
ãçžäºæä»ã°ã«ãŒããç°¡åãªå
¥åãã§ãã¯ãšãšã©ãŒåŠçããã㊠shutil
ã¢ãžã¥ãŒã«ã䜿ã£ãå®éã®ãã¡ã€ã«æäœãçµã¿åãããŠããŸãã
ãã¹ããã©ã¯ãã£ã¹
argparse
ã䜿ã£ãŠå¹æçãªCLIãäœãããã®ãã³ããããã€ã玹ä»ããŸãã
- åããããããã«ãã¡ãã»ãŒãžãæäŸãã:
help
,description
,epilog
ã掻çšãããŠãŒã¶ãŒã䜿ãæ¹ã容æã«ç解ã§ããããã«ããŸããããmetavar
ã§åŒæ°ã®æå³ãæ確ã«ããã®ãæå¹ã§ãã - é©åãªããã©ã«ãå€ãèšå®ãã:
default
ã䜿ã£ãŠãäžè¬çãªãŠãŒã¹ã±ãŒã¹ã§ãŠãŒã¶ãŒããªãã·ã§ã³ãçç¥ã§ããããã«ããŸãã - åãšéžæè¢ãé©åã«äœ¿ã:
type
ãchoices
ã䜿ã£ãŠãäžæ£ãªå ¥åã«å¯ŸããããªããŒã·ã§ã³ãargparse
ã«ä»»ããŸãããã - çã圢åŒãšé·ã圢åŒã®ãªãã·ã§ã³ãæäŸãã:
-v
ãš--verbose
ã®ããã«ããã䜿ããªãã·ã§ã³ã«ã¯äž¡æ¹ã®åœ¢åŒãçšæãããšäŸ¿å©ã§ãã - åŒæ°ãã°ã«ãŒãåãã: é¢é£ããåŒæ°ã¯
add_argument_group()
ã§ãŸãšãããã«ãã¡ãã»ãŒãžãæŽçããŸãã - ãµãã³ãã³ãã掻çšãã: æ©èœãå€å²ã«ãããå Žåã¯ã
add_subparsers()
ã䜿ã£ãŠã³ãã³ãäœç³»ãæ§é åããŸãã - ãšã©ãŒã¡ãã»ãŒãžãæ確ã«ãã:
argparse
ãçæãããšã©ãŒã¡ãã»ãŒãžã¯åœ¹ç«ã¡ãŸãããå¿ èŠã«å¿ããŠparser.error()
ã§ç¬èªã®åããããããšã©ãŒã¡ãã»ãŒãžãè¿œå ããããšãæ€èšããŸãããã - ã³ãŒããæŽçãã: åŒæ°è§£æã®ããžãã¯ïŒããŒãµãŒã®æ§ç¯ïŒãšã解æçµæã䜿ã£ãã¡ã€ã³ã®åŠçããžãã¯ãé¢æ°ãªã©ã§åé¢ãããšãã³ãŒãã®èŠéããè¯ããªããŸãã`if __name__ == ‘__main__’:` ãããã¯å ã§åŠçãåŒã³åºãã®ãäžè¬çã§ãã
ãŸãšã: argparseã§CLIéçºãå¹çåãããïŒ
argparse
ã¯ãPythonã§ã³ãã³ãã©ã€ã³ã€ã³ã¿ãŒãã§ãŒã¹ãéçºããããã®åŒ·åã§æè»ãªæšæºã©ã€ãã©ãªã§ããåºæ¬çãªäœçœ®åŒæ°ããªãã·ã§ã³åŒæ°ã®åŠçãããåãã§ãã¯ãã¢ã¯ã·ã§ã³ãåŒæ°ã°ã«ãŒãããµãã³ãã³ããšãã£ãé«åºŠãªæ©èœãŸã§ãCLIéçºã«å¿
èŠãªå€ãã®æ©èœãæäŸããŠãããŸãã
argparse
ã䜿ãããªãããšã§ã以äžã®ãããªã¡ãªããããããŸãã
- â é¢åãªåŒæ°è§£æããžãã¯ãèªåã§æžãå¿ èŠããªãã
- â
ãã«ãã¡ãã»ãŒãž (
-h
/--help
) ãèªåçæãããã - â äžæ£ãªåŒæ°ã«å¯Ÿãããšã©ãŒãã³ããªã³ã°ãèªåã§è¡ãããã
- â ã³ãŒãã宣èšçã«ãªããã©ã®ãããªåŒæ°ãåãä»ããããåããããããªãã
- â æšæºã©ã€ãã©ãªãªã®ã§è¿œå ã®ã€ã³ã¹ããŒã«ãäžèŠïŒPython 2.7以éïŒã
äžæ¹ã§ãããã·ã³ãã«ãªèšæ³ããããé«åºŠãªæ©èœïŒãªãããªåºåãèªåè£å®ãªã©ïŒãæ±ããå Žåã¯ãClick
ã Typer
ãšãã£ããµãŒãããŒãã£è£œã®ã©ã€ãã©ãªã人æ°ããããŸããããã㯠argparse
ãšã¯ç°ãªãã¢ãããŒãïŒãã³ã¬ãŒã¿ãåãã³ãã掻çšïŒãæäŸããŠããããããžã§ã¯ãã®èŠä»¶ã«ãã£ãŠã¯ããé©ããŠããå ŽåããããŸãã
ããããargparse
ã¯æšæºã©ã€ãã©ãªãšããŠã®å®å®æ§ãšæ®å床ããããå€ãã®å Žé¢ã§ååãªæ©èœãæäŸããŠãããŸãããŸã㯠argparse
ããã£ãããšç解ãã䜿ãããªããããã«ãªãããšããPythonã§ã®CLIéçºã®ç¬¬äžæ©ãšèšããã§ããããð¶ââïžâ¡ïžðââïž
ãã²ãããªãã®æ¬¡ã®Pythonãããžã§ã¯ã㧠argparse
ã掻çšããŠã䜿ããããã³ãã³ãã©ã€ã³ããŒã«ãäœæããŠã¿ãŠãã ããïŒHappy Coding! ð
ã³ã¡ã³ã