CliUtils.java

1
package com.github.dakusui.symfonion.cli;
2
3
import com.github.dakusui.symfonion.compat.exceptions.CliException;
4
import com.github.dakusui.symfonion.song.Keyword;
5
import org.apache.commons.cli.CommandLine;
6
import org.apache.commons.cli.Options;
7
8
import java.io.File;
9
10
import static java.lang.String.format;
11
12
public enum CliUtils {
13
  ;
14
15
  /**
16
   * A method to compose an error message for a specified option.
17
   * This is equivalent to call {@code composeErrMsg(msg, optionName, null}.
18
   *
19
   * @param msg             A message for the option.
20
   * @param shortOptionName A short form of the option.
21
   * @return THe composed message.
22
   */
23
  public static String composeErrMsgForShortOption(String msg, String shortOptionName) {
24 1 1. composeErrMsgForShortOption : replaced return value with "" for com/github/dakusui/symfonion/cli/CliUtils::composeErrMsgForShortOption → SURVIVED
    return composeErrMsgForOption(msg, shortOptionName, null);
25
  }
26
27
  /**
28
   * A method to compose an error message for a specified option.
29
   *
30
   * @param msg             A message for the option.
31
   * @param shortOptionName A short form of the option.
32
   * @param longOptionName  A long form of the option.
33
   * @return The composed message.
34
   */
35
  public static String composeErrMsgForOption(String msg, String shortOptionName, String longOptionName) {
36 1 1. composeErrMsgForOption : negated conditional → KILLED
    if (longOptionName != null) {
37 1 1. composeErrMsgForOption : replaced return value with "" for com/github/dakusui/symfonion/cli/CliUtils::composeErrMsgForOption → KILLED
      return format("(-%s/--%s) %s", shortOptionName, longOptionName, msg);
38
    } else {
39 1 1. composeErrMsgForOption : replaced return value with "" for com/github/dakusui/symfonion/cli/CliUtils::composeErrMsgForOption → KILLED
      return format("(-%s) %s", shortOptionName, msg);
40
    }
41
  }
42
43
  /**
44
   * Returns a value of a specified option.
45
   * This method does not return `null`.
46
   * In case the specified option doesn't have a value, it will throw a `CliException`.
47
   *
48
   * @param cmd        A command line object created by `Cli#parseArgs(...)` method.
49
   * @param optionName An option whose value should be returned.
50
   * @return A value of the option specified by `optionName`.
51
   * @throws CliException Failed to retrieve value for `optionName`. For instance, the value is missing or given more than once.
52
   * @see Cli#parseArgs(Options, String[])
53
   */
54
  static String getSingleOptionValueFromCommandLine(CommandLine cmd, String optionName)
55
  throws CliException {
56
    String ret = cmd.getOptionValue(optionName);
57
    int    sz  = cmd.getOptionProperties(optionName).size();
58 1 1. getSingleOptionValueFromCommandLine : negated conditional → KILLED
    if (sz != 1) {
59
      throw new CliException(composeErrMsgForOption(format("This option requires one and only one value. (found %d times)", sz),
60
                                                    optionName,
61
                                                    null));
62
    }
63 1 1. getSingleOptionValueFromCommandLine : replaced return value with "" for com/github/dakusui/symfonion/cli/CliUtils::getSingleOptionValueFromCommandLine → KILLED
    return ret;
64
  }
65
66
  public static File composeOutputFile(String outfile, String portName) {
67 2 1. composeOutputFile : negated conditional → KILLED
2. composeOutputFile : negated conditional → KILLED
    if (portName == null || Keyword.DEFAULT.toString().equals(portName)) {
68 1 1. composeOutputFile : replaced return value with null for com/github/dakusui/symfonion/cli/CliUtils::composeOutputFile → KILLED
      return new File(outfile);
69
    }
70
    File ret;
71
    int  lastIndexOfDot = outfile.lastIndexOf('.');
72 1 1. composeOutputFile : negated conditional → KILLED
    if (lastIndexOfDot == -1) {
73
      ret = new File(outfile + "." + portName);
74
    } else {
75
      ret = new File(outfile.substring(0, lastIndexOfDot) + "." + portName
76
                     + outfile.substring(lastIndexOfDot));
77
    }
78 1 1. composeOutputFile : replaced return value with null for com/github/dakusui/symfonion/cli/CliUtils::composeOutputFile → KILLED
    return ret;
79
  }
80
}

Mutations

24

1.1
Location : composeErrMsgForShortOption
Killed by : none
replaced return value with "" for com/github/dakusui/symfonion/cli/CliUtils::composeErrMsgForShortOption → SURVIVED
Covering tests

36

1.1
Location : composeErrMsgForOption
Killed by : com.github.dakusui.symfonion.cli.CliUtilsTest.[engine:junit-jupiter]/[class:com.github.dakusui.symfonion.cli.CliUtilsTest]/[method:givenOption_whenComposeErrorMessage_thenMessageComposed()]
negated conditional → KILLED

37

1.1
Location : composeErrMsgForOption
Killed by : com.github.dakusui.symfonion.cli.CliUtilsTest.[engine:junit-jupiter]/[class:com.github.dakusui.symfonion.cli.CliUtilsTest]/[method:givenOption_whenComposeErrorMessage_thenMessageComposed()]
replaced return value with "" for com/github/dakusui/symfonion/cli/CliUtils::composeErrMsgForOption → KILLED

39

1.1
Location : composeErrMsgForOption
Killed by : com.github.dakusui.symfonion.cli.CliUtilsTest.[engine:junit-jupiter]/[class:com.github.dakusui.symfonion.cli.CliUtilsTest]/[method:givenCommandLineGivingOutputOptionTwice_whenGetSingleOptionValueFromCommandLine_thenExceptionThrown()]
replaced return value with "" for com/github/dakusui/symfonion/cli/CliUtils::composeErrMsgForOption → KILLED

58

1.1
Location : getSingleOptionValueFromCommandLine
Killed by : com.github.dakusui.symfonion.cli.CliUtilsTest.[engine:junit-jupiter]/[class:com.github.dakusui.symfonion.cli.CliUtilsTest]/[method:givenCommandLineWithLongOptionName_whenGetSingleOptionValueFromCommandLine_thenValueReturned()]
negated conditional → KILLED

63

1.1
Location : getSingleOptionValueFromCommandLine
Killed by : com.github.dakusui.symfonion.cli.CliUtilsTest.[engine:junit-jupiter]/[class:com.github.dakusui.symfonion.cli.CliUtilsTest]/[method:givenCommandLineWithLongOptionName_whenGetSingleOptionValueFromCommandLine_thenValueReturned()]
replaced return value with "" for com/github/dakusui/symfonion/cli/CliUtils::getSingleOptionValueFromCommandLine → KILLED

67

1.1
Location : composeOutputFile
Killed by : com.github.dakusui.symfonion.cli.CliUtilsTest.[engine:junit-jupiter]/[class:com.github.dakusui.symfonion.cli.CliUtilsTest]/[method:givenNullForPortName_whenComposeOutputFile_thenFileComposedFromGivenName()]
negated conditional → KILLED

2.2
Location : composeOutputFile
Killed by : com.github.dakusui.symfonion.cli.CliUtilsTest.[engine:junit-jupiter]/[class:com.github.dakusui.symfonion.cli.CliUtilsTest]/[method:givenExplicitPortName_whenComposeOutputFile_thenFileComposedFromGivenNameAndPortNam()]
negated conditional → KILLED

68

1.1
Location : composeOutputFile
Killed by : com.github.dakusui.symfonion.cli.CliUtilsTest.[engine:junit-jupiter]/[class:com.github.dakusui.symfonion.cli.CliUtilsTest]/[method:givenNullForPortName_whenComposeOutputFile_thenFileComposedFromGivenName()]
replaced return value with null for com/github/dakusui/symfonion/cli/CliUtils::composeOutputFile → KILLED

72

1.1
Location : composeOutputFile
Killed by : com.github.dakusui.symfonion.cli.CliUtilsTest.[engine:junit-jupiter]/[class:com.github.dakusui.symfonion.cli.CliUtilsTest]/[method:givenExplicitPortName_whenComposeOutputFile_thenFileComposedFromGivenNameAndPortNam()]
negated conditional → KILLED

78

1.1
Location : composeOutputFile
Killed by : com.github.dakusui.symfonion.cli.CliUtilsTest.[engine:junit-jupiter]/[class:com.github.dakusui.symfonion.cli.CliUtilsTest]/[method:givenExplicitPortName_whenComposeOutputFile_thenFileComposedFromGivenNameAndPortNam()]
replaced return value with null for com/github/dakusui/symfonion/cli/CliUtils::composeOutputFile → KILLED

Active mutators

Tests examined


Report generated by PIT 1.19.1