差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
| python:argparse [2020/02/01 19:15] – ともやん | python:argparse [2020/09/11 16:30] (現在) – ともやん | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| - | ====== argparse ====== | + | ====== argparse |
| ===== サンプル ===== | ===== サンプル ===== | ||
| + | <WRAP prewrap 100% # | ||
| <code python args_sample.py> | <code python args_sample.py> | ||
| # | # | ||
| 行 10: | 行 11: | ||
| parser = argparse.ArgumentParser() | parser = argparse.ArgumentParser() | ||
| parser.add_argument(' | parser.add_argument(' | ||
| - | parser.add_argument(' | + | parser.add_argument(' |
| return parser.parse_args() | return parser.parse_args() | ||
| 行 16: | 行 17: | ||
| args = parse_args() | args = parse_args() | ||
| target_dir = args.target_dir | target_dir = args.target_dir | ||
| - | | + | |
| print(f' | print(f' | ||
| - | print(f' | + | print(f' |
| if __name__ == ' | if __name__ == ' | ||
| main() | main() | ||
| - | |||
| </ | </ | ||
| + | </ | ||
| ===== 実行例 ===== | ===== 実行例 ===== | ||
| - | < | + | <WRAP prewrap 100%> |
| + | < | ||
| $ python args_sample.py | $ python args_sample.py | ||
| - | usage: args_sample.py [-h] [--exclude EXCLUDE] target_dir | + | </ |
| + | </ | ||
| + | <WRAP prewrap 100% # | ||
| + | <code powershell> | ||
| + | usage: args_sample.py [-h] [--exclude EXCLUDE | ||
| args_sample.py: | args_sample.py: | ||
| + | </ | ||
| + | </ | ||
| + | <WRAP prewrap 100%> | ||
| + | <code powershell> | ||
| $ python args_sample.py -h | $ python args_sample.py -h | ||
| - | usage: args_sample.py [-h] [--exclude EXCLUDE] target_dir | + | </ |
| + | </ | ||
| + | <WRAP prewrap 100% # | ||
| + | <code powershell> | ||
| + | usage: args_sample.py [-h] [--exclude EXCLUDE | ||
| positional arguments: | positional arguments: | ||
| - | target_dir | + | target_dir |
| optional arguments: | optional arguments: | ||
| - | -h, --help | + | -h, --help |
| - | --exclude EXCLUDE | + | --exclude EXCLUDE |
| + | | ||
| + | </ | ||
| + | </ | ||
| + | <WRAP prewrap 100%> | ||
| + | <code powershell> | ||
| + | $ python args_sample.py / --exclude .git | ||
| + | </ | ||
| + | </ | ||
| + | <WRAP prewrap 100% #result> | ||
| + | <code powershell> | ||
| + | target_dir: / | ||
| + | exclude_dirs: | ||
| </ | </ | ||
| + | </ | ||
| ===== 参考文献 ===== | ===== 参考文献 ===== | ||
| [[https:// | [[https:// | ||
| + | [[https:// | ||