OptionImpl.java

  1. /*
  2.  * Copyright (c) 2016, Stein Eldar Johnsen
  3.  *
  4.  * Licensed to the Apache Software Foundation (ASF) under one
  5.  * or more contributor license agreements. See the NOTICE file
  6.  * distributed with this work for additional information
  7.  * regarding copyright ownership. The ASF licenses this file
  8.  * to you under the Apache License, Version 2.0 (the
  9.  * "License"); you may not use this file except in compliance
  10.  * with the License. You may obtain a copy of the License at
  11.  *
  12.  *   http://www.apache.org/licenses/LICENSE-2.0
  13.  *
  14.  * Unless required by applicable law or agreed to in writing,
  15.  * software distributed under the License is distributed on an
  16.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17.  * KIND, either express or implied. See the License for the
  18.  * specific language governing permissions and limitations
  19.  * under the License.
  20.  */
  21. package net.morimekta.terminal.args.impl;

  22. import net.morimekta.terminal.args.ArgException;
  23. import net.morimekta.terminal.args.Option;

  24. import java.util.List;
  25. import java.util.function.Consumer;

  26. import static java.util.Locale.US;
  27. import static java.util.Objects.requireNonNull;
  28. import static net.morimekta.strings.NamingUtil.splitNameSource;

  29. /**
  30.  * Named option that for each invocation takes a single argument value,
  31.  * either the next argument, or from the same argument after a '='
  32.  * delimiter.
  33.  */
  34. public class OptionImpl extends BaseOption implements Option {
  35.     public static class BuilderImpl implements Option.Builder {
  36.         private final String           name;
  37.         private final String           shortNames;
  38.         private final String           usage;
  39.         private final Consumer<String> consumer;
  40.         private       String           metaVar;
  41.         private       String           defaultValue;
  42.         private       boolean          repeated;
  43.         private       boolean          required;
  44.         private       boolean          hidden;

  45.         public BuilderImpl(String name, String shortNames, String usage, Consumer<String> consumer) {
  46.             this.name = name;
  47.             this.shortNames = shortNames;
  48.             this.usage = usage;
  49.             this.consumer = requireNonNull(consumer, "consumer == null");
  50.             if (shortNames != null) {
  51.                 this.metaVar = String.valueOf(shortNames.charAt(0)).toUpperCase(US);
  52.             } else {
  53.                 var list = splitNameSource(name);
  54.                 this.metaVar = list.get(list.size() - 1).toUpperCase(US);
  55.             }
  56.         }

  57.         @Override
  58.         public Option build() {
  59.             return new OptionImpl(name, shortNames, metaVar, usage, consumer, defaultValue, repeated, required, hidden);
  60.         }

  61.         @Override
  62.         public BuilderImpl metaVar(String metaVar) {
  63.             this.metaVar = metaVar;
  64.             return this;
  65.         }

  66.         @Override
  67.         public BuilderImpl defaultValue(Object object) {
  68.             this.defaultValue = String.valueOf(object);
  69.             return this;
  70.         }

  71.         @Override
  72.         public BuilderImpl repeated() {
  73.             this.repeated = true;
  74.             return this;
  75.         }

  76.         @Override
  77.         public BuilderImpl required() {
  78.             this.required = true;
  79.             return this;
  80.         }

  81.         @Override
  82.         public BuilderImpl hidden() {
  83.             this.hidden = true;
  84.             return this;
  85.         }
  86.     }

  87.     @Override
  88.     public int applyShort(String opts, List<String> args) {
  89.         if (applied && !isRepeated()) {
  90.             throw new ArgException("Option %s already applied", nameOrShort());
  91.         }
  92.         applied = true;

  93.         if (opts.length() == 1) {
  94.             if (args.size() > 1) {
  95.                 setter.accept(args.get(1));
  96.             } else {
  97.                 throw new ArgException("Missing value after -%s", opts);
  98.             }
  99.             return 2;
  100.         } else {
  101.             String value = opts.substring(1);
  102.             setter.accept(value);
  103.             return 1;
  104.         }
  105.     }

  106.     @Override
  107.     public void validate() throws ArgException {
  108.         if (isRequired() && !applied) {
  109.             throw new ArgException("Option %s is required", nameOrShort());
  110.         }
  111.     }

  112.     @Override
  113.     public int apply(List<String> args) throws ArgException {
  114.         if (applied && !isRepeated()) {
  115.             throw new ArgException("Option %s already applied", nameOrShort());
  116.         }
  117.         if (getName() == null) {
  118.             throw new IllegalStateException("No long option for -[" + getShortNames() + "]");
  119.         }
  120.         applied = true;

  121.         String current = args.get(0);
  122.         if (current.startsWith(getName() + "=")) {
  123.             String value = current.substring(getName().length() + 1);
  124.             setter.accept(value);
  125.             return 1;
  126.         } else if (current.equals(getName())) {
  127.             if (args.size() < 2) {
  128.                 throw new ArgException("Missing value after %s", getName());
  129.             }
  130.             setter.accept(args.get(1));
  131.             return 2;
  132.         } else {
  133.             throw new IllegalArgumentException("Argument not matching option " + nameOrShort() + ": " + current);
  134.         }
  135.     }

  136.     private final Consumer<String> setter;

  137.     private boolean applied = false;

  138.     private OptionImpl(String name,
  139.                        String shortNames,
  140.                        String metaVar,
  141.                        String usage,
  142.                        Consumer<String> setter,
  143.                        String defaultValue,
  144.                        boolean repeated,
  145.                        boolean required,
  146.                        boolean hidden) {
  147.         super(name, shortNames, metaVar, usage, defaultValue, repeated, required, hidden);
  148.         this.setter = setter;
  149.     }
  150. }