如果选择了一个选项,则禁用其他选项的Argparse官方方式

时间:2015-10-11 14:38:00

标签: python-3.x argparse

如果-r是,我打算给出彼此不相关的不同行为 是否给定。因此,如果存在-r参数,则只需要两个参数 二。如果没有,它会吃掉所有给定的论点。使用此设置:

parser = ArgumentParser(prog='frog')
parser.add_argument('integer',
                    metavar='integer(s)',
                    type=int,
                    nargs='*')
parser.add_argument('-r', '--range',
                    metavar=('start', 'end'),
                    type=int,
                    nargs=2)

如果我打电话:

frog -r 123 1231 121
# this will give Namespace(integer=[123], range=[1231, 121])

如果我将nargs的{​​{1}}更改为integer,然后致电:

*

当然,创建简单的args检查并不复杂。但是,我的问题是,如果另一个选项被激活,frog -r 123 1231 # complaints about 'integer' that is required 是否有官方方式禁用一个选项?

1 个答案:

答案 0 :(得分:1)

class SingleTapUp : Android.Views.GestureDetector.SimpleOnGestureListener { public override bool OnSingleTapUp(MotionEvent e) { // Toast.MakeText(this,, ToastLength.Long).Show(); return true; } } - 我想你的意思是, public class MainActivity : ActionBarActivity, View.IOnTouchListener { GestureDetector gestureDetector; float _viewX; float _viewY; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); PopulateListView(someList,anynumbertoshow) } private void QueueListView(Queue<FeedItem> feedItemsList, int count) { RelativeLayout rl = this.FindViewById<RelativeLayout>(Resource.Id.newsContainer); if(rl.Visibility == ViewStates.Gone) { this.FindViewById<LinearLayout>(Resource.Id.newsList).Visibility = ViewStates.Gone; rl.Visibility = ViewStates.Visible; } Paint layerPaint = new Paint(); layerPaint.AntiAlias = true; layerPaint.FilterBitmap = true; layerPaint.Dither = true; // RelativeLayout parentLayout = (RelativeLayout)LayoutInflater.from(context).inflate(R.layout.myLayout, null); rl.SetLayerType(LayerType.Hardware, layerPaint); rl.SetClipChildren(false); Random rnd = new Random(); //this.progressDialog.Dismiss(); for (int i = 0; i < count; i++) { FeedItem rss = theNewsQueue.Dequeue(); var viewObj = this.LayoutInflater.Inflate(Resource.Layout.NewTile, rl, false); TextView tv = viewObj.FindViewById<TextView>(Resource.Id.textView2); TextView link = viewObj.FindViewById<TextView>(Resource.Id.link); link.Text = rss.Link; tv.Text = rss.Title; viewObj.Rotation = angle; angle = rnd.Next(-3, 3); viewObj.SetLayerType(LayerType.Hardware, layerPaint); rl.AddView(viewObj); gestureDetector = new GestureDetector(this, new SingleTapUp()); viewObj.SetOnTouchListener(this); //Here I am adding my listener to all my control rl.SetLayerType(LayerType.Hardware, layerPaint); theNewsQueue.Enqueue(rss); rss = null; } } public bool OnTouch(View v, MotionEvent e) { if (gestureDetector.OnTouchEvent(e)) { //will detect a click and open in browser return true; } else { int initialTouchX = 0, initialTouchY = 0; int newx = 0; var x = v.Left; switch (e.Action) { case MotionEventActions.Down: { _viewX = e.GetX(); _viewY = e.GetY(); initialTouchX = (int)e.RawX; initialTouchY = (int)e.RawY; break; } case MotionEventActions.Up: { int lastX = (int)e.GetX(); int lastY = (int)e.GetY(); if ((x - newx) > 40) { //right Swipe sendViewToBack(v); } else if ((newx - x > 40)) { //left Swipe sendViewToBack(v); } break; } case MotionEventActions.Move: { // click = false; var left = (int)(e.RawX - _viewX); newx = left; var right = (int)(left + v.Width); var top = (int)(e.RawY - _viewY); var bottom = (int)(top + v.Height); v.Layout(left, top, right, bottom); break; } } } // _gestureDetector.OnTouchEvent(e); return true; } public void sendViewToBack(View child) { var parent = (ViewGroup)child.Parent; if (null != parent) { parent.RemoveView(child); if(viewType==0) parent.AddView(QueueListView (theNewsQueue), 0); else parent.AddView(QueueListView (theNewsQueue), parent.ChildCount-1); } } } 。在您的示例中,它已经是*。

使用'+',if I change nargs for integer to *, and then call:期望找到至少一个参数。如果change nargs for integer to +没有留下任何遗漏,那么它就会抱怨。

integer的基本哲学是独立处理每个参数。它还尝试与订单无关,因此-r的效果与argparse一样。

可以强制使用自定义frog 123 -r 1231 32类的参数之间的交互,但这往往会破坏订单独立性。

通常最好编写参数以接受最一般的情况,然后自己进行检查。因此,如果frog -r 1231 32 123的值不正确,我会坚持使用Action和对象。

*的主要目的是弄清楚用户想要什么 - 它是一个解析器。你可以随后责骂他们。