python - 如何通过给定参数使用shell脚本为python程序制作linux命令?

标签 python linux shell

例如,我有 python 文件,如 hello.pyadd.pysquare.py。 这些文件的定义是

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

我想从 shell 脚本(例如 pyshell.sh)中执行这些文件,并希望发出如下命令

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

我正在尝试使用这段代码

#! /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)

但是我收到了这样的错误

usage: 1_e.py [-h] {--hai} ...
1_e.py: error: invalid choice: 'name' (choose from '--hai')

最佳答案

这是一个在 python 中使用 argparse 的例子。

您可以通过以下方式运行它:

  • python pyshell.py 你好“你好”
  • python pyshell.py add 20 3.4
  • python pyshell.py square 24

    pyshell.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)
    

关于python - 如何通过给定参数使用shell脚本为python程序制作linux命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43509934/

相关文章:

python - ValueError : Number of features of the model must match the input. 随机森林中模型 n_features 为 10,输入 n_features 为 7

python - 扩展 session 中间件

python - 将字符串写入另一个文件

windows - 在 Ubuntu Linux 中从 Eclipse 交叉编译 Windows 应用程序?

bash - 使用别名更改 scp 命令

java - Bash 中的依赖嵌套 For 循环

python - 'easy_install -U cython' 提示 vcvarsall.bat 和 -mno-cygwin 失败

linux - CygWin 下 Shell 脚本循环内存不足

c - Linux 内核互斥量

regex - 重命名文件夹及其子文件夹的文件