Thread: [te-code-users] Default values for options
Brought to you by:
atownley
|
From: Kevin R. <or...@gm...> - 2005-06-01 10:12:08
|
Hi,
I'm trying to migrate the Perl parts of our application(MillScript)
into Java, using your command line argument parser. I've run into a
problem and would appreciate any help you can offer. We have several
interfaces to MillScript, with various options and my problem lies
with options that take an optional argument, e.g.
website -s
or
website -s /a/path/to/some/folder
In this example, when no argument is specified the application
defaults to using the current folder.
For simplicity I've cut down my example Java implementation to the followin=
g:
import com.townleyenterprises.command.CommandOption;
import com.townleyenterprises.command.CommandParser;
import com.townleyenterprises.command.DefaultCommandListener;
public class OptionalArg {
public static void main( String[] args ) {
CommandOption _option =3D new CommandOption(
"status", 's', true, null, "show status", "."
);
CommandParser _parser =3D new CommandParser( "OptionalArg" );
_parser.setExitOnMissingArg( true, -100 );
_parser.addCommandListener(
new DefaultCommandListener(
"options",
new CommandOption[] {
_option
}
)
);
_parser.parse( args );
}
}
Unfortunately this doesn't work, as the default value for the argument
is only used in the auto help feature. I was wondering if this should
work as I expect it, i.e. the default value is used if no argument is
specified? As it stands, the following happens:
bash-2.05$ java -cp .:te-common.jar OptionalArg -s
error: option --status requires parameter '<arg>'. Exiting.
Usage: OptionalArg [-s|--status <arg>] [-?|--help] [--usage]
Regards,
Kevin
|
|
From: Andrew S. T. <ato...@ei...> - 2005-06-20 09:27:36
|
Kevin,
First off, sincerest apologies for missing this. There was apparently
some issue with me getting mail from the mailing lists. I just noticed
that there were two messages when I went looking to see why I hadn't
seen this before. Thanks for contacting me directly, or I wouldn't have
caught this!
Now, to answer your question:
The original intention of the default value was not to specify a default
parameter substitution, but rather to indicate the value which would be
used <em>if the parameter was not specified</em>.
The net effect is the same, but the semantics are different. Basically,
if the switch is given on the command line, the expectation is that you
will be overriding the default value, not that you expect the default
value to be substituted. I've used this a lot in systems I've written.
If you modify your program slightly:
$ cat OptionalArg.java
import com.townleyenterprises.command.CommandOption;
import com.townleyenterprises.command.CommandParser;
import com.townleyenterprises.command.DefaultCommandListener;
public class OptionalArg {
public static void main( String[] args ) {
CommandOption _option = new CommandOption(
"status", 's', true, null, "show status", "."
);
CommandParser _parser = new CommandParser( "OptionalArg" );
_parser.setExitOnMissingArg( true, -100 );
_parser.addCommandListener(
new DefaultCommandListener(
"options",
new CommandOption[] {
_option
}
)
);
_parser.parse( args );
System.out.println(_option.getArg());
}
}
and then run it, I think you'll notice they system is doing what you
wanted, but not exactly how you expected it to work:
$ java OptionalArg
.
I tried this with the latest release of the library:
$ java com.townleyenterprises.common.Version
TE Common API: 3.0.0-pre3 (Build 83; 2005-02-06 22:15:21 GMT)
I know that a long time ago, the default value was only used for the
display of the autohelp, but that changed at some point. Now, the
default value is evaluated if the argument is not matched, therefore,
you can just use the value in your software without needing to check to
see if it was matched or not.
If this isn't the behavior you're after and you can't modify the way
your command lines are being called, we can think through alternative
solutions.
Let me know if this helps.
Cheers,
ast
On Fri, 2005-06-17 at 13:32, Kevin Rogers wrote:
> Hi,
>
> First off my apologies for emailing you directly, but I haven't had
> any responses via the te-code users mailing list. I ran into a problem
> with default values for command line arguments and would appreciate
> any pointers if I'm trying to do things the wrong way. My original
> message to the mailing list follows:
>
> I'm trying to migrate the Perl parts of our application(MillScript -
> http://www.millscript.org/)
> into Java, using your command line argument parser. I've run into a
> problem and would appreciate any help you can offer. We have several
> interfaces to MillScript, with various options and my problem lies
> with options that take an optional argument, e.g.
>
> website -s
> or
> website -s /a/path/to/some/folder
>
> In this example, when no argument is specified the application
> defaults to using the current folder.
>
> For simplicity I've cut down my example Java implementation to the following:
>
> import com.townleyenterprises.command.CommandOption;
> import com.townleyenterprises.command.CommandParser;
> import com.townleyenterprises.command.DefaultCommandListener;
>
> public class OptionalArg {
>
> public static void main( String[] args ) {
> CommandOption _option = new CommandOption(
> "status", 's', true, null, "show status", "."
> );
> CommandParser _parser = new CommandParser( "OptionalArg" );
> _parser.setExitOnMissingArg( true, -100 );
> _parser.addCommandListener(
> new DefaultCommandListener(
> "options",
> new CommandOption[] {
> _option
> }
> )
> );
> _parser.parse( args );
> }
> }
>
>
> Unfortunately this doesn't work, as the default value for the argument
> is only used in the auto help feature. I was wondering if this should
> work as I expect it, i.e. the default value is used if no argument is
> specified? As it stands, the following happens:
>
> bash-2.05$ java -cp .:te-common.jar OptionalArg -s
> error: option --status requires parameter '<arg>'. Exiting.
> Usage: OptionalArg [-s|--status <arg>] [-?|--help] [--usage]
>
> Regards,
> Kevin
--
Andrew S. Townley <ato...@ei...>
http://atownley.blogspot.com
|
|
From: Kevin R. <or...@gm...> - 2005-06-21 17:57:30
|
Hi Andrew, Thanks for the reply, I understand how you intended the default values to work much better now. I see that the way we have used arguments isn't compatible with this. The command my example comes from is called "website" and it manages aspects of a website directory. When called with the "-s" option, without a parameter, it reports status information for the current directory. If the "-s" option is specified with a parameter, it reports status information for the specified directory. I'm begining to suspect this approach to using arguments was wrong and in this case the directory parameter shouldn't be associated with this option. Rather, I should pick it up via CommandParser.getUnhandledArguments() and make the "-s" option a simple switch. Thanks for the help, Kevin |