Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Rework using -help option to show usage info uniformly for all platforms. #126

Merged
merged 3 commits into from
Jun 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tcases-cli/src/main/java/org/cornutum/tcases/HelpException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//////////////////////////////////////////////////////////////////////////////
//
// Copyright 2020, Cornutum Project
// www.cornutum.org
//
//////////////////////////////////////////////////////////////////////////////

package org.cornutum.tcases;

/**
* Reports a command terminated by a request for help information.
*/
public class HelpException extends RuntimeException
{
private static final long serialVersionUID = 2254436449196262169L;
}
121 changes: 90 additions & 31 deletions tcases-cli/src/main/java/org/cornutum/tcases/ReducerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,17 @@ protected int handleOption( String[] args, int i)
{
String arg = args[i];

if( arg.equals( "-f"))
if( arg.equals( "-help"))
{
throwHelpException();
}

else if( arg.equals( "-f"))
{
i++;
if( i >= args.length)
{
throwUsageException();
throwMissingValue( arg);
}
setFunction( args[i]);
}
Expand All @@ -227,7 +232,7 @@ else if( arg.equals( "-g"))
i++;
if( i >= args.length)
{
throwUsageException();
throwMissingValue( arg);
}
setGenDef( new File( args[i]));
}
Expand All @@ -237,7 +242,7 @@ else if( arg.equals( "-r"))
i++;
if( i >= args.length)
{
throwUsageException();
throwMissingValue( arg);
}
try
{
Expand All @@ -259,7 +264,7 @@ else if( arg.equals( "-s"))
i++;
if( i >= args.length)
{
throwUsageException();
throwMissingValue( arg);
}
try
{
Expand All @@ -281,7 +286,7 @@ else if( arg.equals( "-t"))
i++;
if( i >= args.length)
{
throwUsageException();
throwMissingValue( arg);
}
setTestDef( new File( args[i]));
}
Expand All @@ -291,7 +296,7 @@ else if( arg.equals( "-T"))
i++;
if( i >= args.length)
{
throwUsageException();
throwMissingValue( arg);
}
try
{
Expand All @@ -305,7 +310,7 @@ else if( arg.equals( "-T"))

else
{
throwUsageException();
throwUsageException( String.format( "Unknown option: %s", arg));
}

return i + 1;
Expand All @@ -319,51 +324,101 @@ protected void handleArgs( String[] args, int i)
int nargs = args.length - i;
if( nargs != 1)
{
throwUsageException();
throwUsageException( String.format( "Unexpected argument: %s", args[i+1]));
}

setInputDef( new File( args[i]));
}

/**
* Throws a RuntimeException reporting a command line error.
* Throws a IllegalArgumentException reporting a missing option value
*/
protected void throwUsageException()
protected void throwMissingValue( String option)
{
throwUsageException( null, null);
throwUsageException( String.format( "No value given for %s option", option));
}

/**
* Throws a RuntimeException reporting a command line error.
* Throws a IllegalArgumentException reporting a command line error.
*/
protected void throwUsageException( String detail)
{
throwUsageException( detail, null);
}

/**
* Throws a RuntimeException reporting a command line error.
* Throws a IllegalArgumentException reporting a command line error.
*/
protected void throwUsageException( String detail, Exception cause)
{
if( detail != null)
{
cause = new RuntimeException( detail, cause);
}

throw
new RuntimeException
( "Usage: "
+ ReducerCommand.class.getSimpleName()
+ " [-f function]"
+ " [-g genDef]"
+ " [-r resampleFactor]"
+ " [-R]"
+ " [-s sampleCount]"
+ " [-t testDef]"
+ " [-T contentType]"
+ " inputDef",
cause);
new IllegalArgumentException
( "Invalid command line argument. For all command line details, use the -help option.",
new IllegalArgumentException( detail, cause));
}

/**
* Throws a HelpException after printing usage information to standard error.
*/
protected void throwHelpException()
{
printUsage();
throw new HelpException();
}

/**
* Prints usage information to standard error.
*/
protected void printUsage()
{
for( String line :
new String[] {
"Usage: tcases-reducer [option...] inputDef",
"",
"For a system input definition, updates the associated test case generators to reduce the number",
"of generated test cases, using the given command line options.",
"",
"Each option is one of the following:",
"",
" -f function If -f is defined, update only the test case generator for the given function.",
" Otherwise, update the test case generators for all functions.",
"",
" -g genDef If -g is defined, update the generator specified in the given genDef file.",
" Otherwise, update the default generate definition file: the corresponding",
" *-Generators.xml file in the same directory as the inputDef.",
"",
" -l logFile If -l is defined, log output is written to the given file. If omitted,",
" log output is written to a file named tcases-reducer.log in the current working",
" directory. If logFile is 'stdout', log output is written to standard output.",
"",
" -L logLevel Defines the level for Tcases log output. If omitted, the default level is INFO",
" Tcases logging uses the configuration and levels defined by the Logback system.",
"",
" -r resampleFactor If -r is defined, use the given resampleFactor to determine the number",
" of samples in the next round of reducing. Depending on the resampleFactor,",
" the next round may use more or fewer samples. If the previous round called",
" for N samples and produced a reduction, then the number of samples for the ",
" next round will be N * ( 1 + resampleFactor). To increase sample count with",
" each round, define resampleFactor > 0. To decrease sample count with each round,",
" define -1 < resampleFactor < 0. If resampleFactor is omitted, the default value is 0.",
"",
" -R If defined, ignore any random seed defined in the genDef file and search for a new seed.",
"",
" -s sampleCount Defines the number of samples for the initial round of reducing. If omitted,",
" the default sampleCount is 10.",
"",
" -t testDef If -t is defined, generate test cases based on the test definitions in the",
" specified testDef file, relative to the directory containing the inputDef.",
"",
" -T contentType Defines the default content type for the files read and produced.",
" The contentType must be one of 'json' or 'xml'. The default content type is",
" assumed for any file that is not specified explicitly or that does not have a",
" recognized extension. If omitted, the default content type is derived from the",
" inputDef name."
})
{
System.err.println( line);
}
}

/**
Expand Down Expand Up @@ -656,6 +711,10 @@ public static void main( String[] args)
ReducerCommand reducer = new ReducerCommand();
reducer.run( new Options( args));
}
catch( HelpException h)
{
exitCode = 1;
}
catch( Exception e)
{
exitCode = 1;
Expand Down
Loading