0

Whenever a user doesn't provide suitable parameters to arguments in my script, I raise argparse.ArgumentTypeError with the error string I provided. My problem is that argparse also prints the usage, while it seems irrelevant to me to tell the user how to use my script when he just got a parameter wrong.

Is there a way for certain cases of raising argparse.ArgumentTypeError to suppress the usage printout? Will I need to use a custom usage function? Or maybe I shouldn't be using this exception at all?

4
  • 1
    Have you tried something like parser.add_argument('--foo', help=argparse.SUPPRESS), it's in the documentation. It suppress usage of that command. Read the help section. docs.python.org/2/library/argparse.html#help Commented Jun 2, 2016 at 14:57
  • It suppresses mention of that one argument from the usage message, @Reticentroot. I interpret this question to be about suppressing the entire usage message. Commented Jun 2, 2016 at 15:20
  • So then you just add that argument to all of the input arguments resulting in global suppression @RobKennedy Commented Jun 2, 2016 at 15:22
  • That makes for a lousy message when you actually want the usage message, though, @Reticentroot, such as when the user gives a nonexistent argument, or requests --help. The suppression case is only when a known argument receives an invalid parameter, such as in the documentation where we use parser.add_argument('foo', type=perfect_square), and the user doesn't provide a number that's a perfect square. In that example, we see that the program prints the usage statement and the error message. Gabe wants only the error message. Commented Jun 2, 2016 at 15:27

1 Answer 1

1

Where are you raising the ArgumentTypeError? In a type function?

The definition of ArgumentTypeError is just pass; it doesn't do anything except subclass Exception.

It is referenced in _get_value, which calls the type function on your argument string. That method treats TypeError and ValueError in the same way. It recasts the error as ArgumentError, which adds the argument dest (name) to the message. parse_known_args ends up converting that to a call to parser.error.

It's the parser.error method that adds the print_usage.

So simply changing that method might be the solution to your problem.

https://docs.python.org/3/library/argparse.html#exiting-methods

I'm not sure what happens if your type function returns some other error - it might bubble all the way to the top.

The formal way to change the parser.error method is to subclass ArgumentParser, but you can kludge a one time change with:

In [269]: parser=argparse.ArgumentParser()

In [270]: def myerror(message):
   .....:     print(message)

In [271]: parser.error=myerror

In [272]: parser.add_argument('foo')
Out[272]: _StoreAction(option_strings=[], dest='foo', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)

In [273]: parser.parse_args([])
the following arguments are required: foo
Out[273]: Namespace(foo=None)

This just showed the message, and did not exit. If I'd stuck closer to the original it would have exited as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.