View Javadoc

1   package com.lexicalscope.jewel.cli;
2   
3   import static com.lexicalscope.jewel.cli.parser.DefaultArgumentParserFactory.createDefaultArgumentParser;
4   import static com.lexicalscope.jewel.cli.validation.DefaultValidatorFactory.createDefaultValidator;
5   
6   import com.lexicalscope.fluentreflection.FluentClass;
7   import com.lexicalscope.jewel.cli.specification.CliSpecification;
8   import com.lexicalscope.jewel.cli.specification.OptionsSpecification;
9   
10  /*
11   * Copyright 2011 Tim Wood
12   *
13   * Licensed under the Apache License, Version 2.0 (the "License");
14   * you may not use this file except in compliance with the License.
15   * You may obtain a copy of the License at
16   *
17   * http://www.apache.org/licenses/LICENSE-2.0
18   *
19   * Unless required by applicable law or agreed to in writing, software
20   * distributed under the License is distributed on an "AS IS" BASIS,
21   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   * See the License for the specific language governing permissions and
23   * limitations under the License.
24   */
25  
26  abstract class AbstractCliImpl<O> implements Cli<O> {
27      private final FluentClass<O> klass;
28      private final OptionsSpecification<O> optionsSpecification;
29  
30      public AbstractCliImpl(
31              final FluentClass<O> klass,
32              final OptionsSpecification<O> optionsSpecification) {
33          this.klass = klass;
34          this.optionsSpecification = optionsSpecification;
35      }
36  
37      @Override public O parseArguments(final String... arguments) throws ArgumentValidationException {
38          final ArgumentCollectionBuilder parsedArguments = new ArgumentCollectionBuilder();
39  
40          createDefaultArgumentParser().parseArguments(parsedArguments, arguments);
41  
42          return argumentPresenter(klass, optionsSpecification).presentArguments(parsedArguments.processArguments(createDefaultValidator(optionsSpecification)));
43      }
44  
45      protected abstract ArgumentPresenterImpl<O> argumentPresenter(
46              FluentClass<O> klass,
47              OptionsSpecification<O> specification);
48  
49      @Override public String getHelpMessage() {
50          return optionsSpecification.toString();
51      }
52  
53      CliSpecification getSpecification() {
54          return optionsSpecification;
55      }
56  
57      @Override public void describeTo(final HelpMessage helpMessage) {
58          optionsSpecification.describeTo(helpMessage);
59      }
60  }