Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

So I created a couple of modules: module1.py and module2.py and they're both working fine.

But now I would like to call them from the command line using a script by specifying a corresponding option. For example:

python launchscript.py -l somefile.txt

Or

python launchscript.py -x

First option should sent the parameter and execute main() from module1.py and second option should execute main() from module2.py

This launch script should be extensible as I add more internal modules and it should offer built-in command line help.

I'm a python beginner, any help would be appreciated

share|improve this question
    
You have tagged this argparse - have you tried actually using that library? What exactly is your question? – jonrsharpe Nov 20 '14 at 11:27
    
Correct. I tried to use argparse but to no avail, I'm still lost, so I decided to specify the whole requirement. So taking that into account my question would be how to implement a command line shell from Python? – yllanos Nov 20 '14 at 11:55
1  
Then your question is too broad for SO - this isn't a code-writing or tutorial service. However, if you provided a minimal example of your attempted code and a precise problem description (error traceback, inputs and expected and actual outputs, etc.) that would be on-topic. – jonrsharpe Nov 20 '14 at 11:59
    
Please accept your own answer. – Jubobs Dec 16 '14 at 20:32

I'm responding to myself:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on Nov 19, 2014


@author: yllanos
'''
# Standard libraries
import sys
import argparse

# External libraries

# Product modules
import module1 as dbs
import module2 as gen

def main(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument('-f', '--file', action='store', dest='file',
                help='Does something with a file')
    parser.add_argument('-x', '--xml', action='store_true', dest='xml_switch',
                help='Does some other thing')
    parser.add_argument('-v', '--version', action='version', version='0.1')
    ar = parser.parse_args()

    if ar.file:
        dbs.main(argv[1])
    else:
        None

    if ar.xml_switch:
        gen.main()
    else:
        None

if __name__ == '__main__':
    if len(sys.argv[1:]) == 0:
        print "Usage: ebiller command [OPTION]"
        exit
    else:
        main(sys.argv[1:])
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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