trevorld / r-argparse

command-line optional and positional argument parser
GNU General Public License v2.0
103 stars 11 forks source link

Unclear error with multiline description #44

Closed agilly closed 1 year ago

agilly commented 1 year ago

Using multiline descriptions at initialisation, like:

parser <- ArgumentParser(description="Description of tool.\nAuthor information.")
parser$parse_args()

produces an error:

Error in if (grepl("^\\{|^\\[", output)) { : the condition has length > 1
Calls: <Anonymous> -> parse_args_output
Execution halted

I am not sure you want to support multiline descriptions (it kind of makes sense to IMO?) but in any case the error message should probably be made clearer.

R 4.2.1, python version 3.6.9

trevorld commented 1 year ago

Looking at the behavior of python's argparse module it doesn't support multiline descriptions. However it will quietly convert any newlines into spaces instead of throwing an error.

import argparse

parser = argparse.ArgumentParser(description="Description of tool.\n\nAuthor info")
parser.parse_args()
usage: argparse_test.py [-h]

Description of tool. Author info

optional arguments:
  -h, --help  show this help message and exit

Currently {argparse} is simply a wrapper around Python's argparse module so it wouldn't be straightforward to add multiline descriptions but it is probably feasible to quietly convert those newlines to spaces.

agilly commented 1 year ago

Thanks for the explanation and fix @trevorld !

trevorld commented 1 year ago

After f22b70c46c337 multiline descriptions should work but (like with Python) you'll need to use formatter_class='argparse.RawDescriptionHelpFormatter':

library("argparse")
parser <- ArgumentParser(description="Description of tool.\nAuthor information.", 
                                           formatter_class='argparse.RawDescriptionHelpFormatter')
parser$print_help()
usage: PROGRAM [-h]

Description of tool.
Author information.

optional arguments:
  -h, --help  show this help message and exit