Last active
August 29, 2015 14:12
-
-
Save kkAyataka/ea7a87f69ea2ae647a00 to your computer and use it in GitHub Desktop.
Python ArgumentParser for command line arguments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| import argparse | |
| # とりあえずdescriptionを指定してあれば良い感じ。 | |
| # https://docs.python.org/2.7/library/argparse.html#argparse.ArgumentParser | |
| p = argparse.ArgumentParser(description='command line sample') | |
| # 簡単なオプション類 | |
| p.add_argument('-f', '--flg', action='store_true', help='boolean flag') | |
| p.add_argument('-v', '--val', type=int, help='int value') | |
| p.add_argument('-s', '--str', type=str, help='string value') | |
| # arg.py hogeとした時のhogeが入る | |
| # positional argumentsというらしい | |
| # typeに細工すると、Fileオブジェクトを返してくれる | |
| p.add_argument('tarf', metavar='Target file', type=argparse.FileType('r')) | |
| args = p.parse_args() | |
| print args |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment