trevorld / r-argparse

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

formatter_class for subparsers$add_parser #32

Closed oyxf closed 2 years ago

oyxf commented 2 years ago

Hi,

when I use " formatter_class" to subparsers$add_parser, it seems not work,but for ArgumentParser, it is OK 。

### the script
sub_st_deconv <- subparsers$add_parser("st_deconv", description =docstring ,  formatter_class= "argparse.RawTextHelpFormatter")
## the output logs
Error in .stop(output, "Error: python error") : Error: python error
Traceback (most recent call last):
  File "/public/dev_scRNA/software/miniconda3/envs/SingleCell/lib/python3.7/argparse.py", line 1370, in add_argument
    self._get_formatter()._format_args(action, None)
  File "/public/dev_scRNA/software/miniconda3/envs/SingleCell/lib/python3.7/argparse.py", line 2469, in _get_formatter
    return self.formatter_class(prog=self.prog)
TypeError: 'str' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 262, in <module>
  File "/public/dev_scRNA/software/miniconda3/envs/SingleCell/lib/python3.7/argparse.py", line 1122, in add_parser
    parser = self._parser_class(**kwargs)
  File "/public/dev_scRNA/software/miniconda3/envs/SingleCell/lib/python3.7/argparse.py", line 1675, in __init__
    help=_('show this help message and exit'))
  File "/public/dev_scRNA/software/miniconda3/envs/SingleCell/lib/python3.7/argparse.p
Calls: sapply ... eval -> eval -> intial_setting -> <Anonymous> -> .stop
Execution `halted`
trevorld commented 2 years ago

Please provide a minimal, reproducible example. Thanks!

oyxf commented 2 years ago

Thank you for your fast response!

The example is as follow:

#!/usr/bin/env Rscript
suppressPackageStartupMessages(library("argparse"))
# ======================= GLOBAL command line parameters setting========================================================
parser <- ArgumentParser(
    prog='tools',
    description = " data manipulation toolsets.",
    formatter_class= "lambda prog: argparse.ArgumentDefaultsHelpFormatter(prog, max_help_position=20,width=150)",
    argument_default = "True",
    usage = "%(prog)s [global options]"
)
parser$add_argument(
    "-i",
    "--input",
    type = "character",
    required="True",
    help = "The input exprssion matrix."
)
parser$add_argument(
    "-f",
    "--assay",
    type = "character",
    default = NULL,
    help = "[OPTIONAL]the comma separated assays list except the main assay specified by --assay, when it comes to multimodal assay."
)

subparsers <- parser$add_subparsers(dest="sub_name",
                                    prog ="tools",
                                    metavar = "<subcommand>", 
                                    help = "choose only one option")
sub_st_deconv <- subparsers$add_parser("st_deconv",
                                        formatter_class= "lambda prog: argparse.ArgumentDefaultsHelpFormatter(prog, max_help_position=20,width=150)",
                                        help = "one stop run of data dimension reduction and clustering.") 
sub_st_deconv$add_argument("--refexp", 
                            type = "character",
                            help = "[REQUIRED]the customized reference expression matrix in seurat format, which may come from the quantification results of microarray, Bulk RNA-seq and scRNA-seq sequencing." )
sub_st_deconv$add_argument( "--refcelltype",
                            type = "character",
                            help = "[REQUIRED]the cell type annotation column id in the reference expression matrix." )
opt<-parser$parse_args()

The R version is R. 4.0.3 and command line :

Rscript example.r  --help
trevorld commented 2 years ago

Thanks for the bug report! Should work now in the development version of {argparse}:

remotes::install_github("trevorld/r-argparse")