For example I have python files like hello.py, add.py, square.py.
Definitions of these files are
hello.py:-
def hello(a):
print a #a is some name
add.py:-
def add(a,b):
print a+b #where a,b are numbers which I have to pass as arguments in command
square.py:-
def square(a):
print a**2 #where 'a' is a number
I want to execute these files from shell script(For example pyshell.sh) and want to make commands like
pyshell --hello name - then it has to execute hello.py
pyshell --add 4 5 - then it has to execute add.py
pyshell --square 2 - then it has to execute square.py
I am trying with this code
#! /usr/bin/python
import argparse
# Create Parser and Subparser
parser = argparse.ArgumentParser(description="Example ArgumentParser")
subparser = parser.add_subparsers(help="commands")
# Make Subparsers
hai_parser = subparser.add_parser('--hai', help='hai func')
hai_parser.add_argument("arg",help="string to print")
hai_parser.set_defaults(func='hai')
args = parser.parse_args()
def hai(arg):
print arg
if args.func == '--hai':
hai(args.arg)
But I am getting an error like
usage: 1_e.py [-h] {--hai} ...
1_e.py: error: invalid choice: 'name' (choose from '--hai')
-
1Is a shell script necessary or can you stick with python? You might want to look at the argparse module docs.python.org/3.6/library/argparse.html#module-argparsefizzyh2o– fizzyh2o2017年04月20日 04:05:51 +00:00Commented Apr 20, 2017 at 4:05
-
@ fizzyh2o - Thank you for the reply. Can you explain how to proceed with 'argparse' for the above scenario?sowji– sowji2017年04月20日 04:12:55 +00:00Commented Apr 20, 2017 at 4:12
2 Answers 2
Here's an example using argparse all in python.
You can run it by with the following:
python pyshell.py hello "well hi"python pyshell.py add 20 3.4python pyshell.py square 24pyshell.py:-
import argparse # Create Parser and Subparser parser = argparse.ArgumentParser(description="Example ArgumentParser") subparser = parser.add_subparsers(help="commands") # Make Subparsers hello_parser = subparser.add_parser('hello', help='hello func') hello_parser.add_argument("arg",help="string to print") hello_parser.set_defaults(func='hello') add_parser = subparser.add_parser('add', help="add func") add_parser.add_argument("x",type=float,help='first number') add_parser.add_argument("y",type=float,help='second number') add_parser.set_defaults(func='add') square_parser = subparser.add_parser('square', help="square func") square_parser.add_argument("a",type=float,help='number to square') square_parser.set_defaults(func='square') args = parser.parse_args() def hello(arg): print arg def add(x,y): print x + y def square(a): print a**2 if args.func == 'hello': hello(args.arg) elif args.func == 'add': add(args.x,args.y) elif args.func == 'square': square(args.a)
2 Comments
You can use shebang inside python scripts like #! /usr/bin/python such that those files can be executed like shell script for example python file.py can be executed as file.py if you use shebang. So according to your question you can call those scripts in switch case in shell scripts like this
#! /bin/bash
case ${1:-''} in
"hello")
/path/to/hello 2ドル
;;
"add")
/path/to/add 2ドル 3ドル
;;
"square")
/path/to/square 2ドル
;;
*)
echo "Invalid option supplied"
exit 1
;;
exit 0
If you don't use shebang in python script add python in front of /path/to/script.py better use shebang in script and use absolute path. Also make sure that the respective scripts has execute permissions.