本文介绍了如何从用户输入(raw_input)修改nargs(optparse-add_option的)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是旧问题@的延续:如何访问nargs optparse-add_action?

This Question is continuation of old question @: how to access nargs of optparse-add_action?

回答该问题的目的是什么.

As that question was answered for what it was in-tented.

简介:

假设我使用如下所示的add_option实用程序:

Suppose if I am using add_option utility like below:

parser.add_option('-c','--categories', dest='Categories', nargs=4 )

是否可以使用raw_input从用户输入中修改add_option()nargs ??

Is there a way to modify nargs of add_option() from user input using raw_input.?

我将在先前的问题需求"和此问题的需求"之间给出明显的区别.

I will give a clear difference between my "previous question need" and "this question need".

第一个问题案例:

如果用户未提供任何输入(即他刚刚运行),我的脚本将询问用户输入

My script will ask for user inputs if user has provided no inputs, i.e.,He has just run

#./commandparser.py

第二个问题的案例要求是:

当我运行脚本时./commandparser.py-c abc bac cad它会引发错误:commandparser.py: error: -c option requires 4 arguments并退出脚本.

when i run my script ./commandparser.py -c abc bac cadit throws error: commandparser.py: error: -c option requires 4 arguments and exit the script.

代替抛出错误并退出脚本.我需要某种机制,以便它要求用户输入其余参数,即第4个参数,而无需退出脚本.

Instead of throwing error and exit the script. I want some mechanism so that it asks user to input remaining arguments i.e., 4th argument without exiting the script.

推荐答案

您是否有机会尝试接受此选项的可变数量的值?也就是说,使用"rawinput"设置nargs,然后将其用于解析命令行吗?

Are you, by any chance, trying to accept a variable number of values for this option? That is, use the 'rawinput' to set nargs, which is then used to parse the command line?

optparse文档提供了一个示例,该示例使用自定义回调处理可变数量的值:

The optparse documentation has an example of using a custom callback to handle a variable number of values:

https://docs.python. org/2/library/optparse.html#callback-example-6-variable-arguments

argparse确实允许使用可变数量的值,其中nargs值类似于?". (0或1),"+"(1或更多),"*"(0或更多).

argparse, on the other hand, does allow a variable number of values, with nargs values like '?' (0 or 1), '+' (1 or more), '*' (0 or more).

由于我更熟悉argparse,因此我将草拟一个交互式脚本来满足您的修订要求:

Since I'm more conversant with argparse I'll sketch out an interactive script to handle your revised requirement:

import argparse
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('-c', '--categories', nargs='+', help='4 categories', default=[])
args = parser.parse_args()
print(args)
categories = args.categories
while len(categories)<4:
    print(parser.format_usage())
    x = raw_input('enter %s categories: '%(4-len(categories))).split()
    categories.extend(x)
print('categories', categories)

如果只有类别"作为参数,则可以将所有argparse内容(或optparse)替换为categories = sys.argv[1:],如果仍然需要'-c'标志,则可以替换为[2:].

If 'categories' are the only arguments, you could replace all of the argparse stuff (or optparse) with categories = sys.argv[1:], or [2:] if you still expect the '-c' flag.

或使用optparse(根据docs示例中的可变长度回调改编):

Or using optparse (adapted from the docs example for variable length callback):

def vararg_callback(option, opt_str, value, parser):
     value = []
     for arg in parser.rargs:
         # stop on --foo like options
         if arg[:2] == "--" and len(arg) > 2:
             break
         # stop on -a (ignore the floats issue)
         if arg[:1] == "-" and len(arg) > 1:
             break
         value.append(arg)
     del parser.rargs[:len(value)]
     setattr(parser.values, option.dest, value)

def use_opt():
    import optparse
    parser = optparse.OptionParser()
    parser.add_option('-c','--categories', dest='categories', action="callback", callback=vararg_callback)
    (options, args) = parser.parse_args()
    print options, args
    return options, args, parser

args, rest, parser = use_opt()

这篇关于如何从用户输入(raw_input)修改nargs(optparse-add_option的)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 22:28