6
\$\begingroup\$

We have a main application made of a few separate parts - Unity and Clc.

I'm writing a new tool which will be used to manage them: deploy Unity, or run some checks and a lot of other stuff. As there will be a lot of options, I want split them in to separate namespaces.

Here is my implementation, it works, but I'm not sure if it's the best way.

import argparse

parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()

parser_unity = subparsers.add_parser('unity', help='Unity help')
parser_unity.add_argument('-t', '--tagcheck', dest='unity_tagcheck', action='store_true', help='Check tags in .csproj files')
parser_unity.add_argument('-d', '--deploy', dest='unity_deploy', action='store_true', help='Deploy Unity project to appropriate URL')

parser_clc = subparsers.add_parser('clc', help='Clc help')
parser_clc.add_argument('-d', '--deploy', dest='clc_deploy', action='store_true', help='Deploy Cloudlibrary project to appropriate environment')

res = parser.parse_args()

print('All %s' % res)

try:
    if res.unity_deploy:
        print('Unity deploy')

    if res.unity_tagcheck:
        print('Unity tagcheck')
except AttributeError:
    pass

try:
    if res.clc_deploy:
        print('Clc deploy')
except AttributeError:
    pass

And here is how is works:

d:\RDS\rdsmanager>tmp\test.py clc -d
All Namespace(clc_deploy=True)
Clc deploy

d:\RDS\rdsmanager>tmp\test.py unity -d -t
All Namespace(unity_deploy=True, unity_tagcheck=True)
Unity deploy
Unity tagcheck

d:\RDS\rdsmanager>tmp\test.py unity -d
All Namespace(unity_deploy=True, unity_tagcheck=False)
Unity deploy
\$\endgroup\$
  • \$\begingroup\$ "I don't sure it's correct" - have you tested it? \$\endgroup\$ – jonrsharpe Jun 11 '15 at 9:29
  • \$\begingroup\$ It works (you can see examples in question). But I don't sure I realized it in correct way. May be - there is some better way to do it. \$\endgroup\$ – setevoy Jun 11 '15 at 9:30
  • \$\begingroup\$ Is this unity or unity3d? I'm guessing the later. \$\endgroup\$ – Hosch250 Jun 21 '15 at 5:09
  • \$\begingroup\$ Please wrap at 79 chars - please \$\endgroup\$ – Mr_and_Mrs_D Jun 6 '17 at 18:19
7
\$\begingroup\$

If you take a look at the argparse documentation, they suggest to use set_defaults on each of the parsers to associate a processing function depending on which parser is selected. So if you do that then you can get rid of the try/except blocks, which would improve things quite a lot.

E.g.:

def handler_unity(res):
    if res.unity_deploy:
        print('Unity deploy')

    if res.unity_tagcheck:
        print('Unity tagcheck')

def handler_clc(res):
    if res.clc_deploy:
        print('Clc deploy')
...
parser_unity.set_defaults(func=handler_unity)
...
parser_clc.set_defaults(func=handler_clc)
...
res.func(res)
\$\endgroup\$
  • \$\begingroup\$ Wow... I saw set_default() in docs, but didn't pay attention on it. My fail. Thanks :-) \$\endgroup\$ – setevoy Jun 11 '15 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.