Help gets printed partially on every add_argument call instead of once when needed

Issue #7 invalid
vtd mhd created an issue

MWE:

#!/usr/bin/env Rscript

library('argparser')

p <- arg_parser("desc")



add_argument(p, "a", help="h_arg1", default=1)
add_argument(p, "b", help="h_arg2", default=1)
add_argument(p, "c", help="h_arg3", default=1)

Output:

#!

% ./test.R             
Loading required package: methods
usage: ./test.R [--help] [--opts OPTS] a

desc

positional arguments:
  a                     h_arg1

flags:
  -h, --help                    show this help message and exit

optional arguments:
  -x, --opts OPTS                       RDS file containing argument values
usage: ./test.R [--help] [--opts OPTS] b

desc

positional arguments:
  b                     h_arg2

flags:
  -h, --help                    show this help message and exit

optional arguments:
  -x, --opts OPTS                       RDS file containing argument values
usage: ./test.R [--help] [--opts OPTS] c

desc

positional arguments:
  c                     h_arg3

flags:
  -h, --help                    show this help message and exit

optional arguments:
  -x, --opts OPTS                       RDS file containing argument values

Furthermore, this gets printed even if the arguments are specified

Comments (3)

  1. David Shih repo owner

    R typically does not have side-effects (unless you use lower level functions or write your own C/C++ function.) You need to use an assignment to p to update it.

    p <- add_argument(p, "a", help="h_arg1", default=1)
    p <- add_argument(p, "b", help="h_arg2", default=1)
    p <- add_argument(p, "c", help="h_arg3", default=1)
    

    add_argument returns the an updated copy of p. When you don't assign it to a variable, the updated copy of p gets printed to the console.

    See example in the README.

  2. David Shih repo owner

    I marked the issue as "invalid" because it's not a bug, but I'm happy to help.

    Note that although this package is inspired by the Python argparse package, this package conforms with usual R styles of programming.

  3. Log in to comment