trevorld / r-optparse

command-line optional argument parser
http://trevorldavis.com/R/optparse/dev/
GNU General Public License v2.0
146 stars 11 forks source link

Problems with negative numbers #1

Closed trevorld closed 10 years ago

trevorld commented 11 years ago

Dear Trevor, I think that there is one more thing in the optparse package that can be improved. This is related to the negative numbers as optional arguments after short flags. Such arguments seem not to be allowed in Optparse 1.0.2 (see the code below). However, as the flags always start with a letter character (not a digit according to the manual, Feb 25, 2013), the negative numbers can always be distinguished from flags and therefore they should be parsed as numbers.

library(optparse) optlist = list( make_option(c("-t", "--tmin"), type="numeric", help="Startup time [sec]. ") )

parser = OptionParser(option_list=optlist, usage="", epilogue="")

negative number as optional argument:

parse_args(args=c("--tmin=-11.2"), parser) #this works parse_args(args=c("-t", "-11.2"), parser) #error message (but I think that this should work)

Final note. The original Python Optparse (accordingly to the POSIX specification ?) also behaves somewhat differently in what regards the negative numbers as positional arguments. It requires "-- " before entering negative numbers as positional arguments. But I think that the implementation in R is more intuitive in this case.

http://objectmix.com/python/211112-getopt-negative-numbers.html

R example continued:

negative number as positional argument (works differently than in Python):

parse_args(args=c("--tmin=5.5", "-12"), parser, positional_arguments=TRUE) #this works parse_args(args=c("-t", "0", "-12"), parser, positional_arguments=TRUE) #this works

Python example (from the web page above):

import optparse parser = optparse.OptionParser() parser.add_option("-a", type="int") options, args = parser.parse_args(["-a", "-42", "--", "-123"]) #works options.a args options, args = parser.parse_args(["-a", "-42", "-123"]) #error

I hope that this message can help to improve the Optparse package a little.

Best regards, Miroslav Posta

trevorld commented 10 years ago

User accidently specified "numeric" type rather than "double". Modified program to automatically cast "numeric" to "double".