public AdminTool( CommandLocator commandLocator, BlockerLocator blockerLocator, OutsideWorld outsideWorld, boolean debug ) { this.commandLocator = CommandLocator.withAdditionalCommand( help(), commandLocator ); this.blockerLocator = blockerLocator; this.outsideWorld = outsideWorld; this.debug = debug; this.usage = new Usage( scriptName, this.commandLocator ); }
@Override public void execute( String... args ) throws IncorrectUsage { if ( args.length > 0 ) { try { AdminCommand.Provider commandProvider = this.locator.findProvider( args[0] ); usage.printUsageForCommand( commandProvider, output ); } catch ( NoSuchElementException e ) { StringBuilder validCommands = new StringBuilder(); locator.getAllProviders() .forEach( commandProvider -> validCommands.append( commandProvider.name() ).append( " " ) ); throw new IncorrectUsage( format( "Unknown command: %s. Available commands are: %s\n", args[0], validCommands ) ); } } else { usage.print( output ); } } }
private void printCommands( Consumer<String> output ) { Map<AdminCommandSection,List<AdminCommand.Provider>> groupedProviders = groupProvidersBySection(); AdminCommandSection.general() .printAllCommandsUnderSection( output, groupedProviders.remove( AdminCommandSection.general() ) ); groupedProviders.entrySet().stream() .sorted( Comparator.comparing( groupedProvider -> groupedProvider.getKey().printable() ) ) .forEach(entry -> entry.getKey().printAllCommandsUnderSection( output, entry.getValue() ) ); }
public void print( Consumer<String> output ) { output.accept( format( "usage: %s <command>", scriptName ) ); output.accept( "" ); output.accept( "Manage your Neo4j instance." ); output.accept( "" ); printEnvironmentVariables( output ); output.accept( "available commands:" ); printCommands( output ); output.accept( "" ); output.accept( format( "Use %s help <command> for more details.", scriptName ) ); }
@Test public void shouldPrintNiceHelp() throws Throwable { try ( ByteArrayOutputStream baos = new ByteArrayOutputStream() ) { PrintStream ps = new PrintStream( baos ); Usage usage = new Usage( "neo4j-admin", mock( CommandLocator.class ) ); usage.printUsageForCommand( new SetInitialPasswordCommandProvider(), ps::println ); assertEquals( String.format( "usage: neo4j-admin set-initial-password <password>%n" + "%n" + "environment variables:%n" + " NEO4J_CONF Path to directory which contains neo4j.conf.%n" + " NEO4J_DEBUG Set to anything to enable debug output.%n" + " NEO4J_HOME Neo4j home directory.%n" + " HEAP_SIZE Set JVM maximum heap size during command execution.%n" + " Takes a number and a unit, for example 512m.%n" + "%n" + "Sets the initial password of the initial admin user ('neo4j').%n" ), baos.toString() ); } }
@Test void shouldPrintUsageWithConfiguration() { AdminCommand.Provider[] commands = new AdminCommand.Provider[]{mockCommand( "bam", "A summary", AdminCommandSection.general() )}; final Usage usage = new Usage( "neo4j-admin", new CannedLocator( commands ) ); usage.print( out ); InOrder ordered = inOrder( out ); ordered.verify( out ).accept( "usage: neo4j-admin <command>" ); ordered.verify( out ).accept( "" ); ordered.verify( out ).accept( "Manage your Neo4j instance." ); ordered.verify( out ).accept( "" ); ordered.verify( out ).accept( "environment variables:" ); ordered.verify( out ).accept( " NEO4J_CONF Path to directory which contains neo4j.conf." ); ordered.verify( out ).accept( " NEO4J_DEBUG Set to anything to enable debug output." ); ordered.verify( out ).accept( " NEO4J_HOME Neo4j home directory." ); ordered.verify( out ).accept( " HEAP_SIZE Set JVM maximum heap size during command execution." ); ordered.verify( out ).accept( " Takes a number and a unit, for example 512m." ); ordered.verify( out ).accept( "" ); ordered.verify( out ).accept( "available commands:" ); ordered.verify( out ).accept( "General" ); ordered.verify( out ).accept( " bam" ); ordered.verify( out ).accept( " A summary" ); ordered.verify( out ).accept( "" ); ordered.verify( out ).accept( "Use neo4j-admin help <command> for more details." ); ordered.verifyNoMoreInteractions(); }
private void badUsage( AdminCommand.Provider command, IncorrectUsage e ) { outsideWorld.stdErrLine( e.getMessage() ); outsideWorld.stdErrLine( "" ); usage.printUsageForCommand( command, outsideWorld::stdErrLine ); failure(); }
private void badUsage( String message ) { outsideWorld.stdErrLine( message ); usage.print( outsideWorld::stdErrLine ); failure(); }
void printDetailed( Consumer<String> output ) { for ( Arguments arguments : command.possibleArguments() ) { String left = format( "usage: %s %s", scriptName, command.name() ); output.accept( Arguments.rightColumnFormatted( left, arguments.usage(), left.length() + 1 ) ); } output.accept( "" ); Usage.printEnvironmentVariables( output ); output.accept( command.allArguments().description( command.description() ) ); } }
@Test void shouldPrintUsageForACommand() { // given AdminCommand.Provider commandProvider = mockCommand( "bam", "A summary", AdminCommandSection.general() ); AdminCommand.Provider[] commands = new AdminCommand.Provider[]{commandProvider}; final Usage usage = new Usage( "neo4j-admin", new CannedLocator( commands ) ); // when usage.printUsageForCommand( commandProvider, out ); // then InOrder ordered = inOrder( out ); ordered.verify( out ).accept( "usage: neo4j-admin bam " ); ordered.verify( out ).accept( "" ); ordered.verify( out ).accept( "environment variables:" ); ordered.verify( out ).accept( " NEO4J_CONF Path to directory which contains neo4j.conf." ); ordered.verify( out ).accept( " NEO4J_DEBUG Set to anything to enable debug output." ); ordered.verify( out ).accept( " NEO4J_HOME Neo4j home directory." ); ordered.verify( out ).accept( " HEAP_SIZE Set JVM maximum heap size during command execution." ); ordered.verify( out ).accept( " Takes a number and a unit, for example 512m." ); ordered.verify( out ).accept( "" ); ordered.verify( out ).accept( "description" ); }
@Test void commandsUnderSameAdminCommandSectionPrintableSectionShouldAppearTogether() { AdminCommand.Provider[] commands = new AdminCommand.Provider[]{ mockCommand( "first-command", "first-command", AdminCommandSection.general() ), mockCommand( "second-command", "second-command", new TestGeneralSection() )}; final Usage usage = new Usage( "neo4j-admin", new CannedLocator( commands ) ); usage.print( out ); InOrder ordered = inOrder( out ); ordered.verify( out ).accept( "usage: neo4j-admin <command>" ); ordered.verify( out ).accept( "" ); ordered.verify( out ).accept( "Manage your Neo4j instance." ); ordered.verify( out ).accept( "" ); ordered.verify( out ).accept( "environment variables:" ); ordered.verify( out ).accept( " NEO4J_CONF Path to directory which contains neo4j.conf." ); ordered.verify( out ).accept( " NEO4J_DEBUG Set to anything to enable debug output." ); ordered.verify( out ).accept( " NEO4J_HOME Neo4j home directory." ); ordered.verify( out ).accept( " HEAP_SIZE Set JVM maximum heap size during command execution." ); ordered.verify( out ).accept( " Takes a number and a unit, for example 512m." ); ordered.verify( out ).accept( "" ); ordered.verify( out ).accept( "available commands:" ); ordered.verify( out ).accept( "General" ); ordered.verify( out ).accept( " first-command" ); ordered.verify( out ).accept( " first-command" ); ordered.verify( out ).accept( " second-command" ); ordered.verify( out ).accept( " second-command" ); ordered.verify( out ).accept( "" ); ordered.verify( out ).accept( "Use neo4j-admin help <command> for more details." ); ordered.verifyNoMoreInteractions(); }
usage.printUsageForCommand( provider, outsideWorld::stdErrLine ); failure();
@Test public void shouldPrintNiceHelp() throws Throwable { try ( ByteArrayOutputStream baos = new ByteArrayOutputStream() ) { PrintStream ps = new PrintStream( baos ); Usage usage = new Usage( "neo4j-admin", mock( CommandLocator.class ) ); usage.printUsageForCommand( new SetDefaultAdminCommandProvider(), ps::println ); assertEquals( String.format( "usage: neo4j-admin set-default-admin <username>%n" + "%n" + "environment variables:%n" + " NEO4J_CONF Path to directory which contains neo4j.conf.%n" + " NEO4J_DEBUG Set to anything to enable debug output.%n" + " NEO4J_HOME Neo4j home directory.%n" + " HEAP_SIZE Set JVM maximum heap size during command execution.%n" + " Takes a number and a unit, for example 512m.%n" + "%n" + "Sets the user to become admin if users but no roles are present, for example%n" + "when upgrading to neo4j 3.1 enterprise.%n" ), baos.toString() ); } }
HelpCommand helpCommand = new HelpCommand( new Usage( "neo4j-admin", commandLocator ), ps::println, commandLocator ); helpCommand.execute( "foobar" );
@Test public void shouldPrintNiceHelp() throws Exception { try ( ByteArrayOutputStream baos = new ByteArrayOutputStream() ) { PrintStream ps = new PrintStream( baos ); Usage usage = new Usage( "neo4j-admin", mock( CommandLocator.class ) ); usage.printUsageForCommand( new StoreInfoCommandProvider(), ps::println ); assertEquals( String.format( "usage: neo4j-admin store-info --store=<path-to-dir>%n" + "%n" + "environment variables:%n" + " NEO4J_CONF Path to directory which contains neo4j.conf.%n" + " NEO4J_DEBUG Set to anything to enable debug output.%n" + " NEO4J_HOME Neo4j home directory.%n" + " HEAP_SIZE Set JVM maximum heap size during command execution.%n" + " Takes a number and a unit, for example 512m.%n" + "%n" + "Prints information about a Neo4j database store, such as what version of Neo4j%n" + "created it. Note that this command expects a path to a store directory, for%n" + "example --store=data/databases/graph.db.%n" + "%n" + "options:%n" + " --store=<path-to-dir> Path to database store.%n" ), baos.toString() ); } }
Usage usage = new Usage( "neo4j-admin", commandLocator );
@Test void shouldPrintNiceHelp() throws Exception { try ( ByteArrayOutputStream baos = new ByteArrayOutputStream() ) { PrintStream ps = new PrintStream( baos ); Usage usage = new Usage( "neo4j-admin", mock( CommandLocator.class ) ); usage.printUsageForCommand( new DumpCommandProvider(), ps::println ); assertEquals( String.format( "usage: neo4j-admin dump [--database=<name>] --to=<destination-path>%n" + "%n" + "environment variables:%n" + " NEO4J_CONF Path to directory which contains neo4j.conf.%n" + " NEO4J_DEBUG Set to anything to enable debug output.%n" + " NEO4J_HOME Neo4j home directory.%n" + " HEAP_SIZE Set JVM maximum heap size during command execution.%n" + " Takes a number and a unit, for example 512m.%n" + "%n" + "Dump a database into a single-file archive. The archive can be used by the load%n" + "command. <destination-path> can be a file or directory (in which case a file%n" + "called <database>.dump will be created). It is not possible to dump a database%n" + "that is mounted in a running Neo4j server.%n" + "%n" + "options:%n" + " --database=<name> Name of database. [default:" + GraphDatabaseSettings.DEFAULT_DATABASE_NAME + "]%n" + " --to=<destination-path> Destination (file or folder) of database dump.%n" ), baos.toString() ); } }
sc=new Scanner(System.in); String input=""; while((input=sc.nextLine())!=null) { String[] arr=input.split(" "); int numberI=Integer.parseInt(arr[0]); Usage usage=new Usage(arr[1]); computerNames[numberI].addObsseration(usage); } sc.close();
Usage usage = new Usage( "neo4j-admin", mock( CommandLocator.class ) ); usage.printUsageForCommand( new LoadCommandProvider(), ps::println );
int calls = myLineScan2.nextInt(); int txt = myLineScan2.nextInt(); usages.add((new Usage(custID, calls, txt)));