/** * Returns true if this argument was given explicitly on the command line */ default boolean has( Args parsedArgs ) { return parsedArgs.has( name() ); } }
BackupOutcome run( String[] args ) throws ToolFailureException { Args arguments = Args.withFlags( VERIFY ).parse( args ); if ( !arguments.hasNonNull( TO ) ) { throw new ToolFailureException( "Specify target location with " + dash( TO ) + " <target-directory>" ); } if ( arguments.hasNonNull( FROM ) && !arguments.has( HOST ) && !arguments.has( PORT ) ) { return runBackupWithLegacyArgs( arguments ); } else if ( arguments.hasNonNull( HOST ) ) { return runBackup( arguments ); } else { throw new ToolFailureException( NO_SOURCE_SPECIFIED ); } }
@Deprecated @SafeVarargs public final <T> T interpretOption( String key, Function<String,T> defaultValue, Function<String,T> converter, Validator<T>... validators ) { T value; if ( !has( key ) ) { value = defaultValue.apply( key ); } else { String stringValue = get( key ); value = converter.apply( stringValue ); } return validated( value, validators ); }
private static long parsePid( Args args ) throws CommandFailed { if ( args.has( PID_KEY ) ) { try { return Long.parseLong( args.get( PID_KEY, "" ) ); } catch ( NumberFormatException e ) { throw new CommandFailed( "Unable to parse --" + PID_KEY, e ); } } return NO_PID; }
if ( args.has( "list" ) )
@Override public void execute( String[] stringArgs ) throws IncorrectUsage, CommandFailed { Args args = Args.withFlags( "list", "to", "verbose", "force", PID_KEY ).parse( stringArgs ); verbose = args.has( "verbose" ); jmxDumper = new JMXDumper( homeDir, fs, out, err, verbose ); pid = parsePid( args ); boolean force = args.has( "force" ); DiagnosticsReporter reporter = createAndRegisterSources(); Optional<Set<String>> classifiers = parseAndValidateArguments( args, reporter ); if ( !classifiers.isPresent() ) { return; } DiagnosticsReporterProgress progress = buildProgress(); // Start dumping Path destinationDir = new File( destinationArgument.parse( args ) ).toPath(); try { Path reportFile = destinationDir.resolve( getDefaultFilename() ); out.println( "Writing report to " + reportFile.toAbsolutePath().toString() ); reporter.dump( classifiers.get(), reportFile, progress, force ); } catch ( IOException e ) { throw new CommandFailed( "Creating archive failed", e ); } }
@Test void testInterleavedEqualsArgsAndSplitKeyValue() { String[] line = { "-host=localhost", "-v", "--port", "1234", "param1", "-name=Something", "param2" }; Args args = Args.parse( line ); assertEquals( "localhost", args.get( "host", null ) ); assertTrue( args.has( "v" ) ); assertEquals( 1234, args.getNumber( "port", null ).intValue() ); assertEquals( "Something", args.get( "name", null ) ); assertEquals( 2, args.orphans().size() ); assertEquals( "param1", args.orphans().get( 0 ) ); assertEquals( "param2", args.orphans().get( 1 ) ); }
@Test void testInterleavedParametersWithValuesAndNot() { String[] line = { "-host", "machine.foo.com", "-port", "1234", "-v", "-name", "othershell" }; Args args = Args.parse( line ); assertEquals( "machine.foo.com", args.get( "host", null ) ); assertEquals( "1234", args.get( "port", null ) ); assertEquals( 1234, args.getNumber( "port", null ).intValue() ); assertEquals( "othershell", args.get( "name", null ) ); assertTrue( args.has( "v" ) ); assertTrue( args.orphans().isEmpty() ); }
private void dumpMessage( PrintStream out, PrintStream err ) { String message = cause.getCause() instanceof ConnectException ? "Connection refused" : cause.getMessage(); err.println( "ERROR (-v for expanded information):\n\t" + message ); if ( args.has( "v" ) ) { cause.printStackTrace( err ); } err.println(); printUsage( out ); } }
@Deprecated @SafeVarargs public final <T> T interpretOption( String key, Function<String,T> defaultValue, Function<String,T> converter, Validator<T>... validators ) { T value; if ( !has( key ) ) { value = defaultValue.apply( key ); } else { String stringValue = get( key ); value = converter.apply( stringValue ); } return validated( value, validators ); }
private static long parsePid( Args args ) throws CommandFailed { if ( args.has( PID_KEY ) ) { try { return Long.parseLong( args.get( PID_KEY, "" ) ); } catch ( NumberFormatException e ) { throw new CommandFailed( "Unable to parse --" + PID_KEY, e ); } } return NO_PID; }
if ( args.has( "list" ) )
@Override public void execute( String[] stringArgs ) throws IncorrectUsage, CommandFailed { Args args = Args.withFlags( "list", "to", "verbose", "force", PID_KEY ).parse( stringArgs ); verbose = args.has( "verbose" ); jmxDumper = new JMXDumper( homeDir, fs, out, err, verbose ); pid = parsePid( args ); boolean force = args.has( "force" ); DiagnosticsReporter reporter = createAndRegisterSources(); Optional<Set<String>> classifiers = parseAndValidateArguments( args, reporter ); if ( !classifiers.isPresent() ) { return; } DiagnosticsReporterProgress progress = buildProgress(); // Start dumping Path destinationDir = new File( destinationArgument.parse( args ) ).toPath(); try { Path reportFile = destinationDir.resolve( getDefaultFilename() ); out.println( "Writing report to " + reportFile.toAbsolutePath().toString() ); reporter.dump( classifiers.get(), reportFile, progress, force ); } catch ( IOException e ) { throw new CommandFailed( "Creating archive failed", e ); } }