private void validate() throws IncorrectUsage { for ( String o : parsedArgs.asMap().keySet() ) { if ( !namedArgs.containsKey( o ) ) { throw new IncorrectUsage( format( "unrecognized option: '%s'", o ) ); } } long mandatoryPositionalArgs = positionalArgs.stream() .filter( o -> o instanceof MandatoryPositionalArgument ) .count(); if ( parsedArgs.orphans().size() < mandatoryPositionalArgs ) { throw new IncorrectUsage( "not enough arguments" ); } String excessArgs = parsedArgs.orphans().stream() .skip( positionalArgs.size() ) .collect( Collectors.joining( " " ) ); if ( !excessArgs.isEmpty() ) { throw new IncorrectUsage( format( "unrecognized arguments: '%s'", excessArgs ) ); } }
private static boolean asksForUsage( Args args ) { for ( String orphan : args.orphans() ) { if ( isHelpKey( orphan ) ) { return true; } } for ( Entry<String,String> option : args.asMap().entrySet() ) { if ( isHelpKey( option.getKey() ) ) { return true; } } return false; }
public static Args useArgumentsFromFileArgumentIfPresent( Args args ) throws IOException { String fileArgument = args.get( Options.FILE.key(), null ); if ( fileArgument != null ) { // Are there any other arguments supplied, in addition to this -f argument? if ( args.asMap().size() > 1 ) { throw new IllegalArgumentException( "Supplying arguments in addition to " + Options.FILE.argument() + " isn't supported." ); } // Read the arguments from the -f file and use those instead args = Args.parse( parseFileArgumentList( new File( fileArgument ) ) ); } return args; }
@Test void shouldGetAsMap() { // GIVEN Args args = Args.parse( "--with-value", "value", "--without-value" ); // WHEN Map<String,String> map = args.asMap(); // THEN assertEquals( stringMap( "with-value", "value", "without-value", null ), map ); }
@Test void testParameterWithDashValue() { String [] line = { "-file", "-" }; Args args = Args.parse( line ); assertEquals( 1, args.asMap().size() ); assertEquals( "-", args.get( "file", null ) ); assertTrue( args.orphans().isEmpty() ); }
static Map<String,Serializable> getSessionVariablesFromArgs( Args args ) { String profile = args.get( "profile", null ); Map<String,Serializable> session = new HashMap<>(); if ( profile != null ) { applyProfileFile( new File( profile ), session ); } for ( Map.Entry<String,String> entry : args.asMap().entrySet() ) { String key = entry.getKey(); if ( key.startsWith( "D" ) ) { key = key.substring( 1 ); session.put( key, entry.getValue() ); } } if ( isCommandLine( args ) ) { session.put( "quiet", true ); } return session; }
private static boolean asksForUsage( Args args ) { for ( String orphan : args.orphans() ) { if ( isHelpKey( orphan ) ) { return true; } } for ( Entry<String,String> option : args.asMap().entrySet() ) { if ( isHelpKey( option.getKey() ) ) { return true; } } return false; }
public static Args useArgumentsFromFileArgumentIfPresent( Args args ) throws IOException { String fileArgument = args.get( Options.FILE.key(), null ); if ( fileArgument != null ) { // Are there any other arguments supplied, in addition to this -f argument? if ( args.asMap().size() > 1 ) { throw new IllegalArgumentException( "Supplying arguments in addition to " + Options.FILE.argument() + " isn't supported." ); } // Read the arguments from the -f file and use those instead args = Args.parse( parseFileArgumentList( new File( fileArgument ) ) ); } return args; }