named mandatory arguments

13 views
Skip to first unread message

Tomas Neme

unread,
Oct 28, 2013, 3:58:24 PM10/28/13
to argh-...@googlegroups.com
How can I add a named mandatory command? if for a "deploy" command I do this:

def deploy(manifest):
'''help'''

when I call

$ mybin deploy

I get a "too few arguments" error. Besides the point that I'd like a
more personalized error message, my problem is that if I call

$ mybin deploy /foo/bar

it works, and I want the '-m' or the '--manifest' to be mandatory

If I change that to

@argh.arg('--manifest', nargs=1)
def deploy(manifest):
'''help'''

then

$ mybin deploy

calls deploy with manifest=None

IIRC, I can do what I want with ('-m', '--manifest', nargs=1) in argparse

--
"The whole of Japan is pure invention. There is no such country, there
are no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.

Tomas Neme

unread,
Oct 28, 2013, 5:11:23 PM10/28/13
to argh-...@googlegroups.com
disregard that, I remembered about mandatory=True

Andy Mikhailenko

unread,
Oct 29, 2013, 2:23:02 AM10/29/13
to argh-...@googlegroups.com
> I'd like a more personalized error message

The "too few arguments" message is generated by argparse. The message
may vary between the versions of Python.
I think Argh should be as transparent as possible. Keeping the
exceptions intact is part of this policy. Of course, if there's a
chance to fix the problem, it should be done, but in this case I don't
see how Argh could be smarter than the underlying parser without
introducing additional complexity.

As to the point, I don't think I completely understood your
intentions, but generally `nargs=N` should be used with care; if an
*optional* argument requires at least one value, it's not optional
anymore and *should* be (though not must be) declared as positional.

Andy

Tomas Neme

unread,
Oct 29, 2013, 9:32:43 AM10/29/13
to argh-...@googlegroups.com
> The "too few arguments" message is generated by argparse. The message

yeah, I know. I was just thinking argparse should have a way of
defining the error message when an argument is missing, to avoid the
piece of boilerplate where I put default=None to everything and then
check and raise

> As to the point, I don't think I completely understood your
> intentions, but generally `nargs=N` should be used with care; if an
> *optional* argument requires at least one value, it's not optional
> anymore and *should* be (though not must be) declared as positional.

Yes, what I wanted to do is to have an argument be required, but to
force the user to call it with the --switch syntax. As I said above,
calling @arg(...., required=True) does exactly that
Reply all
Reply to author
Forward
0 new messages