Skip to content

Instantly share code, notes, and snippets.

@GitSumito
Created December 21, 2017 13:23
Show Gist options
  • Save GitSumito/0f4722c892d631142d7ca2f1f26b9ccd to your computer and use it in GitHub Desktop.
Save GitSumito/0f4722c892d631142d7ca2f1f26b9ccd to your computer and use it in GitHub Desktop.
Pythonでparse_args後、ハイフンからアンダーバーに置き換わる。 ref: https://qiita.com/S-T/items/49febd9488a49efd8c9c
Example execution:
python mailer.py --query-id 2226 --query-api-key 6764573853b4065aac3258b8c7bc561dc4179092 --org-slug default \
--smtp-username "..." --smtp-password '...' --smtp-host 'smtp.mailgun.org' \
--sender 'Arik <[email protected]>' --to '[email protected]'
--subject "Test" --body-html-file html --body-text-file text
parser = argparse.ArgumentParser(description='Mail Re:dash CSV query by email.')
parser.add_argument('--smtp-username')
parser.add_argument('--smtp-password')
parser.add_argument('--smtp-host')
parser.add_argument('--sender', help='From (sender) address')
parser.add_argument('--to', help='To address(es)', nargs='+')
parser.add_argument('--bcc', help='Bcc address(es)', nargs='*', default=[])
parser.add_argument('--subject', help='Subject line.')
parser.add_argument('--body-html-file', help='Body html file path.')
parser.add_argument('--body-text-file', help='Body text file path.')
parser.add_argument('--org-slug', help='Query id to export')
parser.add_argument('--query-id', help='Query id to export')
parser.add_argument('--query-api-key', help='API key for the query')
args = parser.parse_args()
query_csv = get_query_csv(args.org_slug, args.query_id, args.query_api_key)
$ python ./test.py --username tsukada --query-id 123 --query-api-key abcd123
tsukada
123
abcd123
$
$ python ./test.py --username tsukada --query-id 123 --query-api-key abcd123
tsukada
123
Traceback (most recent call last):
File "./test.py", line 12, in <module>
print(args.query-api-key)
AttributeError: 'Namespace' object has no attribute 'query'
$
import argparse
parser = argparse.ArgumentParser(description='Sample code for python.')
parser.add_argument('--username')
parser.add_argument('--query-id', help='Query id to export')
parser.add_argument('--query-api-key', help='API key for the query')
args = parser.parse_args()
print(args.username)
print(args.query_id)
print(args.query_api_key)
import argparse
parser = argparse.ArgumentParser(description='Sample code for python.')
parser.add_argument('--username')
parser.add_argument('--query-id', help='Query id to export')
parser.add_argument('--query-api-key', help='API key for the query')
args = parser.parse_args()
print(args.username)
print(args.query_id)
print(args.query-api-key)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment