docopt.R icon indicating copy to clipboard operation
docopt.R copied to clipboard

Arguments have hidden positional dependency

Open eyzhao opened this issue 8 years ago • 4 comments

Hello there,

Thank you for bringing this phenomenal tool to R!

I'm on docopt_0.4.5 and I've noticed a strange behaviour regarding order of arguments. Here's an example:

' Usage: combine_germline_files.R -p PATHS -o OUTPUT [ -n GENENAME ]
 ' -> doc

 library(docopt)
 args = docopt(doc)

When I run

Rscript scripts/pipelines/pog-germline/combine_germline_files.R -p paths/germline_paths.txt -o meta/mutyh_germline.txt -n MUTYH

it works just fine, but if I reverse the argument order of -p and -o as follows:

Rscript scripts/pipelines/pog-germline/combine_germline_files.R -o meta/mutyh_germline.txt -p paths/germline_paths.txt -n MUTYH

I get an error. The script loads meta/mutyh_germline into PATHS instead of correctly loading paths/germline_paths.txt into PATHS. This seems to suggest a hidden dependency on the order of arguments, not just which flags they follow.

eyzhao avatar Jul 12 '17 00:07 eyzhao

@eyzhao I would have written that opt string differently: (and this works fine)

library(docopt)
'Usage: 
  test.R -a <A> -b <B>

Options:
 -a <A>  option 1
 -b <B>  option 2
' -> doc
docopt(doc, args = "-a x -b y" )
docopt(doc, args = "-b y -a x" )

shntnu avatar Sep 01 '17 00:09 shntnu

Thanks shntnu,

Follow-up question. If there's a more complex set of arguments, would that necessitate encoding every possible ordering of parameters? Or is there a more elegant solution?

eyzhao avatar Sep 01 '17 17:09 eyzhao

Ah, but the version I have above is order invariant i.e.

docopt(doc, args = "-a x -b y" )
docopt(doc, args = "-b y -a x" )

give the same result.

shntnu avatar Sep 01 '17 17:09 shntnu

Oh! My apologies. I misunderstood those last two lines. Yes, I have been sure to include the "Options:" section recently and glad to know that this addresses the issue. Thanks for your help :)

eyzhao avatar Sep 01 '17 17:09 eyzhao