LoginSignup
3
3

More than 5 years have passed since last update.

Pythonでparse_args後、ハイフンからアンダーバーに置き換わる。

Posted at

Gitに上がっているredashのソースを読んでいるうちに
arikfr氏が書いたre:dash結果をCSVでメールで送信するスクリプトを見つけた。

ソースを追っていて、「なんでこーなるの?」って所があったので
メモ代わりにここに残す事にした。
ちなみにPythonは他のプログラム同様「なんとなく読める」程度のレベル。
Pythonで何かを作ったことはない。

疑問

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 <arik@redash.io>' --to 'arik@redash.io'
                 --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)

あれ?
--query-api-key で受け取ったのが、query_api_keyになっている。
ちゃんと動くの?

サンプルスクリプトを作って試してみる

test.py
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)

$ python ./test.py  --username tsukada --query-id 123 --query-api-key abcd123
tsukada
123
abcd123
$ 

あら。普通にウマく行った。parse_argsを行うとハイフンがアンダーバーに変換されるのか。

試しにパースされた後、ハイフンで受け取ろうとした。

ダメなtest.py
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)
$ 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'
$

ということでちゃんとパース後はハイフンからアンダースコアに置きかえないとコケる。
Hello World以来のPython、地道に頑張ろう。

3
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3