如何通过执行作为字符串输入的指令将值传递给python函数?

时间:2015-07-24 17:21:42

标签: python command-line-arguments

我有一个功能:

def foo(a=0, b=0, c=0, val=0, someotherval=0):  
    print val + someotherval  

在文件bar.py内调用此函数。当我从控制台运行bar.py时,我想将参数作为字符串传递给函数:

>>python bar.py "val=3"  

因此函数foo将其解释为:

foo(val=3)  

我试图使用exec命令。在我的bar.py文件中:

import sys
cmdlinearg = sys.argv[1]    # capturing commandline argument
foo(exec(cmdlinearg))  

但是我收到语法错误 我知道我可以自己传递参数值,但是使用带有许多参数的函数,我不希望最终用户为不需要的参数输入0:

>>python bar.py "0" "0" "0" "3"  

有没有办法实现这个目标?

5 个答案:

答案 0 :(得分:2)

我宁愿以正确的方式执行此操作并使用argparse。

您的命令行界面如下所示:

bar.py --a 0 --b 0 --c 0 --val 0 --someotherval 0

代码就是这样:

import argparse

def main():
  parser = argparse.ArgumentParser()
  parser.add_argument('a', type=int, default=0)
  ...
  args = parser.parse_args()
  foo(a=args.a, b=args.b, c=args.c, var=args.val, someotherval=args.someotherval)

if __name__ == '__main__':
  main()

答案 1 :(得分:1)

如果你不关心安全问题,你可以这样做

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#define string char*
int main(int argc, string argv[])
{
int a = argc;
if (a != 2)
{
    return 1;
}

string b = argv[1];
//b IS THE CODE WORD
string ptext;
//ptext IS THE MESSAGE TO BE ENCRYPTED

if (isalpha(b))
{
    //ptext = GetString();
    ptext = "Hello World";
}
else
return 1;

//this is to index the letters within the code word
int c = 0;
int d;
for (c = 0, d = strlen(b); c < d; c++)
{
//int k = (b(char[c]));
int k = *(b+c);
    //this is to index the letters within the plaintext
    int i = 0;
    int l;
    for (i = 0, l = strlen(ptext); i < l ;i++)
    {
        //This will cause buffer overflow, so commenting
        /*
        while (isalpha(ptext[i]))
        {
        printf("%c", b[c]%d+k);  //what are you trying to print as %d here
        }
        */
    }
}
}

或者这个:

exec('foo(%s)' % sys.argv[1])

但是,如果您关注的是用户的易用性,也许您应该看看argparse

答案 2 :(得分:1)

如何使用argparse解析命令行参数?

示例 -

import argparse

def foo(a=0, b=0, c=0, val=0, someotherval=0):  
    print(val + someotherval)

parser = argparse.ArgumentParser(description='Some Parser')
parser.add_argument('-a','--a',default=0,type=int,help="value for a")
parser.add_argument('-b','--b',default=0,type=int,help="value for a")
parser.add_argument('-c','--c',default=0,type=int,help="value for a")
parser.add_argument('-v','--val',default=0,type=int,help="value for a")
parser.add_argument('-s','--someotherval',default=0,type=int,help="value for a")
args = parser.parse_args()
foo(a=args.a,b=args.b,c=args.c,val=args.val,someotherval=args.someotherval)

然后你可以调用并得到像 -

这样的结果
>python a.py
0

>python a.py --val 10
10

>python a.py --val 10 --someotherval 100
110

答案 3 :(得分:0)

看看这个库: http://click.pocoo.org

在您的情况下可能会有用。

import click

@click.command()
@click.option('-a', default=0)
@click.option('-b', default=0)
@click.option('-c', default=0)
@click.option('--val', default=0)
@click.option('--someotherval', default=0)
def foo(a, b, c, val, someotherval):  
    print val + someotherval

if __name__ == '__main__':
    foo()

但是,您必须添加use options作为参数,而不是字符串:

>> python bar.py --val=3  

答案 4 :(得分:-2)

如果您知道他们永远不会输入其他参数,您可以默认其他参数的值。

foo(0,0,exec(cmdlinearg),0)