trevorld / r-argparse

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

Support for 'parents' argument of 'ArgumentParser' #28

Open paulahsan opened 5 years ago

paulahsan commented 5 years ago

Hi, As I used two different functions to parse the arguments in python successfully following below:

def getRequiredArgs():
        paser = argparse.ArgumentParser()
        parser.add_argumet('--file', help="...")
        return parser
def getOptionalArgs():
        paser = argparse.ArgumentParser()
        parser.add_argumet('--operator', help="...")
        return parser
def allParser():
        parser = argparse.ArgumentParser(
        description="ALL ARGUMENST",
        parents = (getRequiredArgs(), getOptionalArgs())
         )
        return parser

I tried similar fashion with Rscript. What I did in R is:

getRequiredArgs <- function () {
      parser <- ArgumentParser("...")
      parser$add_argument("--file", help="....")
      return parser
}
getOptionalArgs <- function () {
      parser <- ArgumentParser("...")
      parser$add_argument("--operator", help="....")
      return parser
}
getAllParser <- function () {
      parser <- ArgumentParser("ALL PARSER",
      # These are the attempts I have taken
      1. parents = (getRequiredArgs(), getOptionalArgs()) 
      2. parents = (getRequiredArgs() + getOptionalArgs())
      3. parents = (c(getRequiredArgs(), getOptionalArgs())
      4. parents = (list(getRequiredArgs(), getOptionalArgs()))
      5. parents = list(c(getRequiredArgs(), getOptionalArgs()))
      6. parser <- getRequiredParser()
         parser <- append(parser, getOptionalParser())
)
      return parser
}

and finally, even following this

getAllParser <- function () {
        getRequiredArgs() + getOptionalArgs()
}

However, none of them worked for me. I could not merge the Required and Optional arguments. Is it I am doing wrong or it is not still possible in r-argparse? To be noted, for the getOptionalArgs I used several Options groups, i.e add_argument_group.

trevorld commented 5 years ago

Currently it is not possible to append/add together two r-argparse parser objects to get a new parser object combining each parsers arguments.

However the following approach should work:

addRequiredArgs <- function(parser) {
    parser$add_argument("file", help="....")
    invisible(NULL)
}
addOptionalArgs <- function(parser) {
    parser$add_argument("--operator", help="....")
    invisible(NULL)
}
getAllParser <- function() {
    parser <- ArgumentParser(description="ALL PARSER")
    addRequiredArgs(parser)
    addOptionalArgs(parser)
    parser
}

Edit: Added in description= to ArgumentParser call.

paulahsan commented 5 years ago

Sorry mate, I cannot make this workable I tried like following:

#!/usr/local/bin/Rscript
suppressWarnings(library(argparse))

addRequiredArgs <- function(parser) {
  parser$add_argument("--file", help="....")
  invisible(NULL)
}
addOptionalArgs <- function(parser) {
  parser$add_argument("--operator", help="....")
  invisible(NULL)
}
getAllParser <- function() {
  parser <- ArgumentParser("ALL PARSER")
  addRequiredArgs(parser)
  addOptionalArgs(parser)
  return(parser)
}
executeMain <- function() {
  parser <- getAllParser()
  args <- parser$parse_args()
}

if(!interactive()) {
  executeMain()
}

The output is like:

Rscript --vanilla parent_parser.R -h
Error in .stop(message, "syntax error:") : syntax error:
Positional argument following keyword argument. Please note ``ArgumentParser`` only accepts keyword arguments.
Calls: executeMain -> <Anonymous> -> .stop
Execution halted

In another attempt I tried the simpler version suggested by you:

#!/usr/local/bin/Rscript

suppressWarnings(library(argparse))

addRequiredArgs <- function(parser) {
  parser$add_argument("--file", help="....")
  invisible(NULL)
}
addOptionalArgs <- function(parser) {
  parser$add_argument("--operator", help="....")
  invisible(NULL)
}
getAllParser <- function() {
  parser <- ArgumentParser("ALL PARSER")
  addRequiredArgs(parser)
  addOptionalArgs(parser)
  return(parser)
}
 getAllParser()

The output:

Rscript --vanilla parent_parser.R -h
<Parser>
  Public:
    add_argument: function (...)
    add_argument_group: function (...)
    add_mutually_exclusive_group: function (required = FALSE)
    add_subparsers: function (...)
    clone: function (deep = FALSE)
    initialize: function (python_code, name)
    parse_args: function (args = commandArgs(TRUE))
    print_help: function ()
    print_usage: function ()
  Private:
    n_groups: 0
    n_mutually_exclusive_groups: 0
    name: parser
    python_code: Code, R6
trevorld commented 5 years ago
#!/usr/local/bin/Rscript
suppressWarnings(library("argparse"))

addRequiredArgs <- function(parser) {
  parser$add_argument("file", help="file option help")
  invisible(NULL)
}
addOptionalArgs <- function(parser) {
  parser$add_argument("--operator", help="operator option help")
  invisible(NULL)
}
getAllParser <- function() {
  parser <- ArgumentParser(description="ALL PARSER")
  addRequiredArgs(parser)
  addOptionalArgs(parser)
  return(parser)
}

parser <- getAllParser()
args <- parser$parse_args()
print(args)
trevorld@stoic-sloth:~/tmp$ Rscript test.R --help
usage: test.R [-h] [--operator OPERATOR] file

ALL PARSER

positional arguments:
  file                 file option help

optional arguments:
  -h, --help           show this help message and exit
  --operator OPERATOR  operator option help
trevorld@stoic-sloth:~/tmp$ Rscript test.R boo.txt
$file
[1] "boo.txt"

$operator
NULL

trevorld@stoic-sloth:~/tmp$ Rscript test.R --operator=foo boo.txt
$file
[1] "boo.txt"

$operator
[1] "foo"
paulahsan commented 5 years ago

Thanks a lot ! That worker perfectly. I should have been more careful regarding the error message:

Please note ``ArgumentParser`` only accepts keyword arguments