@Test
  public void testInjectionOfString() throws CLIException {
    HelloClI command = new HelloClI();
    CLI cli = CLIConfigurator.define(HelloClI.class);
    CommandLine evaluatedCLI = cli.parse(Arrays.asList("--name", "vert.x"));
    CLIConfigurator.inject(evaluatedCLI, command);

    assertThat(command.run()).isEqualToIgnoringCase("Hello vert.x");
    assertThat(command.name).isEqualToIgnoringCase("vert.x");
  }
示例#2
0
 @Test
 public void retrievingExtraArguments() throws ParsingException {
   cli =
       new CLI() {
         {
           operand("input");
         }
       };
   Args args = cli.parse("input", "output", "encoding");
   assertEquals(asList("output", "encoding"), args.others());
 }
示例#3
0
 @Test
 public void specifyingADefaultValueForAnOption() throws Exception {
   cli =
       new CLI() {
         {
           option("-b").takingArgument("SIZE").ofType(int.class).defaultingTo(1024);
         }
       };
   cli.parse();
   assertEquals(1024, cli.get("-b"));
 }
示例#4
0
 @Test
 public void usingSimpleOptionSwitches() throws ParsingException {
   cli =
       new CLI() {
         {
           flag("-x").describedAs("Turns debugging on");
         }
       };
   cli.parse("-x");
   assertTrue(cli.has("-x"));
 }
示例#5
0
 @Test
 public void detectingAnUnrecognizedOption() throws Exception {
   cli = new CLI();
   try {
     cli.parse("--whatever");
     fail("Expected exception " + UnrecognizedOptionException.class.getName());
   } catch (UnrecognizedOptionException expected) {
     assertEquals("--whatever", expected.getOption());
     assertThat(expected.getMessage(), containsString("whatever"));
   }
 }
示例#6
0
 @Test
 public void specifyingTheTypeOfAnOptionArgument() throws Exception {
   cli =
       new CLI() {
         {
           option("-b").takingArgument("SIZE").ofType(int.class);
         }
       };
   cli.parse("-b", "1024");
   int blockSize = cli.<Integer>get("-b");
   assertEquals(1024, blockSize);
 }
示例#7
0
 @Test
 public void definingAnOptionWithAnAliasForm() throws Exception {
   cli =
       new CLI() {
         {
           option("-x").alias("--debug");
         }
       };
   cli.parse("--debug");
   assertTrue(cli.has("-x"));
   assertTrue(cli.has("--debug"));
 }
示例#8
0
 @Test
 public void usingACustomOptionType() throws Exception {
   cli =
       new CLI() {
         {
           coerceType(BigDecimal.class).using(new BigDecimalCoercer());
           option("--size VALUE").ofType(BigDecimal.class);
         }
       };
   cli.parse("--size", "1000.00");
   assertEquals(new BigDecimal("1000.00"), cli.get("--size"));
 }
示例#9
0
  @Test
  public void specifyingTheTypeOfAPositionalArgument() throws ParsingException {
    cli =
        new CLI() {
          {
            operand("input").ofType(File.class);
          }
        };
    cli.parse("/path/to/input");

    File inputFile = cli.get("input");
    assertEquals("/path/to/input", inputFile.getAbsolutePath());
  }
示例#10
0
 @Test
 public void specifyingAnOptionInLiteralForm() throws Exception {
   cli =
       new CLI() {
         {
           option("-h", "--host HOSTNAME", "Hostname to bind to");
         }
       };
   cli.parse("--host", "0.0.0.0");
   assertTrue(cli.has("-h"));
   assertTrue(cli.has("--host"));
   assertEquals("0.0.0.0", cli.get("-h"));
 }
示例#11
0
  @Test
  public void executingACallbackWhenAnOptionIsDetected() throws Exception {
    final CaptureLocale captureLocale = new CaptureLocale();
    cli =
        new CLI() {
          {
            option("-l LOCALE").ofType(Locale.class).whenPresent(captureLocale);
          }
        };

    cli.parse("-l", "FR");
    assertEquals(Locale.FRENCH, captureLocale.locale);
  }
示例#12
0
 @Test
 public void usingBuiltInCoercers() throws Exception {
   cli =
       new CLI() {
         {
           option("-c").takingArgument("CLASSNAME").ofType(Class.class);
           operand("file").ofType(File.class);
         }
       };
   cli.parse("-c", "java.lang.String", "/path/to/file");
   assertEquals(String.class, cli.get("-c"));
   assertEquals(new File("/path/to/file"), cli.get("file"));
 }
示例#13
0
  @Test
  public void definingAnOptionThatExpectsAnArgument() throws Exception {
    cli =
        new CLI() {
          {
            option("-b").takingArgument("SIZE");
          }
        };
    cli.parse("-b", "1024");

    assertTrue(cli.has("-b"));
    assertEquals("1024", cli.get("-b"));
  }
示例#14
0
  @Test
  public void usingPositionalArguments() throws Exception {
    cli =
        new CLI() {
          {
            operand("input").describedAs("The input file");
            operand("output").describedAs("The output file");
          }
        };
    cli.parse("input", "output");

    assertEquals("input", cli.get("input"));
    assertEquals("output", cli.get("output"));
  }
示例#15
0
  @Test
  public void retrievingArgumentsInATypeSafeWay() throws ParsingException {
    cli = new CLI();
    ArgumentSpec<Boolean> verbose = cli.option("-v").ofType(Boolean.class);
    ArgumentSpec<Integer> size =
        cli.option("--block-size").takingArgument("SIZE").ofType(int.class).defaultingTo(1024);
    OperandSpec<File> input = cli.operand("input").ofType(File.class);
    Args args = cli.parse("-v", "--block-size", "2048", "/path/to/input");

    File inputFile = input.get(args);
    assertEquals("/path/to/input", inputFile.getAbsolutePath());
    boolean verboseFlag = verbose.get(args);
    assertEquals(true, verboseFlag);
    int blockSize = size.get(args);
    assertEquals(2048, blockSize);
  }
示例#16
0
 @Test
 public void omittingARequiredOptionArgument() throws Exception {
   cli =
       new CLI() {
         {
           option("-b SIZE").ofType(int.class);
         }
       };
   try {
     cli.parse("-b");
     fail("Expected exception " + ArgumentMissingException.class.getName());
   } catch (ArgumentMissingException expected) {
     assertEquals("-b", expected.getUnsatisfiedOption());
     assertThat(expected.getMessage(), containsString("-b"));
   }
 }
示例#17
0
 @Test
 public void omittingARequiredPositionalArgument() throws Exception {
   cli =
       new CLI() {
         {
           operand("input");
           operand("output");
         }
       };
   try {
     cli.parse("input");
     fail("Expected exception " + MissingOperandException.class.getName());
   } catch (MissingOperandException expected) {
     assertEquals("output", expected.getMissingOperand());
     assertThat(expected.getMessage(), containsString("output"));
   }
 }
示例#18
0
 @Test
 public void usingAnInvalidPositionalArgument() throws Exception {
   cli =
       new CLI() {
         {
           operand("size").ofType(int.class);
         }
       };
   try {
     cli.parse("LITERAL");
     fail("Expected exception " + InvalidArgumentException.class.getName());
   } catch (InvalidArgumentException expected) {
     assertEquals("size", expected.getUnsatisfiedArgument());
     assertEquals("LITERAL", expected.getOffendingValue());
     assertThat(expected.getMessage(), containsString("size"));
     assertThat(expected.getMessage(), containsString("LITERAL"));
   }
 }
示例#19
0
文件: Main.java 项目: yurius-r/decaf
  public static void main(String[] args) {
    try {
      CLI.parse(args, new String[0]);

      InputStream inputStream =
          args.length == 0 ? System.in : new java.io.FileInputStream(CLI.infile);

      if (CLI.target == CLI.SCAN) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        Token token;
        boolean done = false;
        while (!done) {
          try {
            for (token = lexer.nextToken();
                token.getType() != DecafParserTokenTypes.EOF;
                token = lexer.nextToken()) {
              String type = "";
              String text = token.getText();

              switch (token.getType()) {
                case DecafScannerTokenTypes.ID:
                  type = " IDENTIFIER";
                  break;
              }
              System.out.println(token.getLine() + type + " " + text);
            }
            done = true;
          } catch (Exception e) {
            // print the error:
            System.out.println(CLI.infile + " " + e);
            lexer.consume();
          }
        }
      } else if (CLI.target == CLI.PARSE || CLI.target == CLI.DEFAULT) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        DecafParser parser = new DecafParser(lexer);
        parser.program();
      }

    } catch (Exception e) {
      // print the error:
      System.out.println(CLI.infile + " " + e);
    }
  }
示例#20
0
  @Test
  public void aMoreComplexExampleThatUsesAMixOfDifferentArguments() throws Exception {
    cli =
        new CLI() {
          {
            flag("-h").describedAs("Human readable format");
            option("-b").alias("--block-size").takingArgument("SIZE").ofType(int.class);
            flag("-x");
            operand("input").as("INFILE").describedAs("The input file");
            operand("output", "OUTFILE", "The output file");
          }
        };

    cli.parse("-h", "--block-size", "1024", "-x", "input", "output", "extra", "more extra");
    assertEquals(6, cli.options().size());
    assertTrue(cli.has("-h"));
    assertEquals(1024, cli.get("--block-size"));
    assertTrue(cli.has("-x"));
    assertEquals("input", cli.get("input"));
    assertEquals("output", cli.get("output"));
    assertEquals(asList("extra", "more extra"), cli.others());
  }
示例#21
0
  public static void main(String[] args) {
    try {
      CLI.parse(args, new String[0]);

      InputStream inputStream =
          args.length == 0 ? System.in : new java.io.FileInputStream(CLI.infile);

      if (CLI.target == CLI.SCAN) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        Token token;
        boolean done = false;
        while (!done) {
          try {
            for (token = lexer.nextToken();
                token.getType() != DecafParserTokenTypes.EOF;
                token = lexer.nextToken()) {
              String type = "";
              String text = token.getText();

              switch (token.getType()) {
                case DecafScannerTokenTypes.ID:
                  type = " IDENTIFIER";
                  break;
                case DecafScannerTokenTypes.CHAR:
                  type = " CHARLITERAL";
                  break;
                case DecafScannerTokenTypes.TRUE:
                case DecafScannerTokenTypes.FALSE:
                  type = " BOOLEANLITERAL";
                  break;
                case DecafScannerTokenTypes.HEX:
                case DecafScannerTokenTypes.DECIMAL:
                  type = " INTLITERAL";
                  break;
                case DecafScannerTokenTypes.STRING:
                  type = " STRINGLITERAL";
                  break;
              }
              System.out.println(token.getLine() + type + " " + text);
            }
            done = true;
          } catch (Exception e) {
            // print the error:
            System.out.println(CLI.infile + " " + e);
            lexer.consume();
          }
        }
      } else if (CLI.target == CLI.PARSE || CLI.target == CLI.DEFAULT) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        DecafParser parser = new DecafParser(lexer, CLI.debug);
        // DecafParser parser = new DecafParser (lexer);
        parser.program();
      } else if (CLI.target == CLI.INTER) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        DecafParser parser = new DecafParser(lexer, CLI.debug);
        parser.program();
        IrNode irRoot = parser.getIrTree();
        SemanticChecker checker = new SemanticChecker(CLI.infile, CLI.debug);
        if (CLI.debug) System.out.println("--- checking -----");
        checker.checkProgram((IrClassDecl) irRoot);
      } else if (CLI.target == CLI.LOWIR) {
        DecafScanner lexer = new DecafScanner(new DataInputStream(inputStream));
        DecafParser parser = new DecafParser(lexer, CLI.debug);
        parser.program();
        IrNode irRoot = parser.getIrTree();
        SemanticChecker checker = new SemanticChecker(CLI.infile, CLI.debug);
        if (CLI.debug) System.out.println("--- checking -----");
        checker.checkProgram((IrClassDecl) irRoot);
        CodeGen codegen = new CodeGen(irRoot, CLI.debug);
        codegen.genLowIr();
        codegen.printLowIr();
      }
    } catch (Exception e) {
      // print the error:
      System.out.println(CLI.infile + " " + e);
    }
  }
 private CommandLine parse(CLI cli, String... args) throws CLIException {
   return cli.parse(Arrays.asList(args));
 }