use of com.google.devtools.common.options.OptionsBase in project bazel by bazelbuild.
the class BlazeRuntime method parseOptions.
/**
* Parses the command line arguments into a {@link OptionsParser} object.
*
* <p>This function needs to parse the --option_sources option manually so that the real option
* parser can set the source for every option correctly. If that cannot be parsed or is missing,
* we just report an unknown source for every startup option.
*/
private static OptionsProvider parseOptions(Iterable<BlazeModule> modules, List<String> args) throws OptionsParsingException {
ImmutableList<Class<? extends OptionsBase>> optionClasses = BlazeCommandUtils.getStartupOptions(modules);
// First parse the command line so that we get the option_sources argument
OptionsParser parser = OptionsParser.newOptionsParser(optionClasses);
parser.setAllowResidue(false);
parser.parse(OptionPriority.COMMAND_LINE, null, args);
Function<? super String, String> sourceFunction = sourceFunctionForMap(parser.getOptions(BlazeServerStartupOptions.class).optionSources);
// Then parse the command line again, this time with the correct option sources
parser = OptionsParser.newOptionsParser(optionClasses);
parser.setAllowResidue(false);
parser.parseWithSourceFunction(OptionPriority.COMMAND_LINE, sourceFunction, args);
return parser;
}
use of com.google.devtools.common.options.OptionsBase in project bazel by bazelbuild.
the class CanonicalizeCommand method exec.
@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
BlazeRuntime runtime = env.getRuntime();
Options canonicalizeOptions = options.getOptions(Options.class);
String commandName = canonicalizeOptions.forCommand;
BlazeCommand command = runtime.getCommandMap().get(commandName);
if (command == null) {
env.getReporter().handle(Event.error("Not a valid command: '" + commandName + "' (should be one of " + Joiner.on(", ").join(runtime.getCommandMap().keySet()) + ")"));
return ExitCode.COMMAND_LINE_ERROR;
}
Collection<Class<? extends OptionsBase>> optionsClasses = BlazeCommandUtils.getOptions(command.getClass(), runtime.getBlazeModules(), runtime.getRuleClassProvider());
try {
OptionsParser parser = OptionsParser.newOptionsParser(optionsClasses);
parser.setAllowResidue(false);
parser.parse(options.getResidue());
InvocationPolicyEnforcer invocationPolicyEnforcer = InvocationPolicyEnforcer.create(canonicalizeOptions.invocationPolicy);
invocationPolicyEnforcer.enforce(parser, commandName);
List<String> result = parser.canonicalize();
for (String piece : result) {
env.getReporter().getOutErr().printOutLn(piece);
}
} catch (OptionsParsingException e) {
env.getReporter().handle(Event.error(e.getMessage()));
return ExitCode.COMMAND_LINE_ERROR;
}
return ExitCode.SUCCESS;
}
use of com.google.devtools.common.options.OptionsBase in project bazel by bazelbuild.
the class DumpCommand method exec.
@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
BlazeRuntime runtime = env.getRuntime();
DumpOptions dumpOptions = options.getOptions(DumpOptions.class);
boolean anyOutput = dumpOptions.dumpPackages || dumpOptions.dumpVfs || dumpOptions.dumpActionCache || dumpOptions.dumpRuleClasses || (dumpOptions.dumpSkyframe != SkyframeDumpOption.OFF);
if (!anyOutput) {
Map<String, String> categories = new HashMap<>();
categories.put("verbosity", "Options that control what internal state is dumped");
Collection<Class<? extends OptionsBase>> optionList = new ArrayList<>();
optionList.add(DumpOptions.class);
env.getReporter().getOutErr().printErrLn(BlazeCommandUtils.expandHelpTopic(getClass().getAnnotation(Command.class).name(), getClass().getAnnotation(Command.class).help(), getClass(), optionList, categories, OptionsParser.HelpVerbosity.LONG, runtime.getProductName()));
return ExitCode.ANALYSIS_FAILURE;
}
PrintStream out = new PrintStream(env.getReporter().getOutErr().getOutputStream());
try {
out.println("Warning: this information is intended for consumption by developers");
out.println("only, and may change at any time. Script against it at your own risk!");
out.println();
boolean success = true;
if (dumpOptions.dumpPackages) {
env.getPackageManager().dump(out);
out.println();
}
if (dumpOptions.dumpVfs) {
out.println("Filesystem cache");
FileSystemUtils.dump(env.getOutputBase().getFileSystem(), out);
out.println();
}
if (dumpOptions.dumpActionCache) {
success &= dumpActionCache(env, out);
out.println();
}
if (dumpOptions.dumpRuleClasses) {
dumpRuleClasses(runtime, out);
out.println();
}
if (dumpOptions.dumpSkyframe != SkyframeDumpOption.OFF) {
success &= dumpSkyframe(env.getSkyframeExecutor(), dumpOptions.dumpSkyframe == SkyframeDumpOption.SUMMARY, out);
out.println();
}
return success ? ExitCode.SUCCESS : ExitCode.ANALYSIS_FAILURE;
} finally {
out.flush();
}
}
use of com.google.devtools.common.options.OptionsBase in project bazel by bazelbuild.
the class ConfigSetting method matchesConfig.
/**
* Given a list of [flagName, flagValue] pairs, returns true if flagName == flagValue for
* every item in the list under this configuration, false otherwise.
*/
private boolean matchesConfig(Map<String, String> expectedSettings, BuildConfiguration config) throws OptionsParsingException {
// Rather than returning fast when we find a mismatch, continue looking at the other flags
// to check that they're indeed valid flag specifications.
boolean foundMismatch = false;
// Since OptionsParser instantiation involves reflection, let's try to minimize that happening.
Map<Class<? extends OptionsBase>, OptionsParser> parserCache = new HashMap<>();
for (Map.Entry<String, String> setting : expectedSettings.entrySet()) {
String optionName = setting.getKey();
String expectedRawValue = setting.getValue();
Class<? extends OptionsBase> optionClass = config.getOptionClass(optionName);
if (optionClass == null) {
throw new OptionsParsingException("unknown option: '" + optionName + "'");
}
OptionsParser parser = parserCache.get(optionClass);
if (parser == null) {
parser = OptionsParser.newOptionsParser(optionClass);
parserCache.put(optionClass, parser);
}
parser.parse("--" + optionName + "=" + expectedRawValue);
Object expectedParsedValue = parser.getOptions(optionClass).asMap().get(optionName);
if (!optionMatches(config, optionName, expectedParsedValue)) {
foundMismatch = true;
}
}
return !foundMismatch;
}
use of com.google.devtools.common.options.OptionsBase in project bazel by bazelbuild.
the class HelpCommand method emitCompletionHelp.
private void emitCompletionHelp(BlazeRuntime runtime, OutErr outErr) {
// First startup_options
Iterable<BlazeModule> blazeModules = runtime.getBlazeModules();
ConfiguredRuleClassProvider ruleClassProvider = runtime.getRuleClassProvider();
Map<String, BlazeCommand> commandsByName = getSortedCommands(runtime);
outErr.printOutLn("BAZEL_COMMAND_LIST=\"" + SPACE_JOINER.join(commandsByName.keySet()) + "\"");
outErr.printOutLn("BAZEL_INFO_KEYS=\"");
for (String name : InfoCommand.getHardwiredInfoItemNames(runtime.getProductName())) {
outErr.printOutLn(name);
}
outErr.printOutLn("\"");
outErr.printOutLn("BAZEL_STARTUP_OPTIONS=\"");
Iterable<Class<? extends OptionsBase>> options = BlazeCommandUtils.getStartupOptions(blazeModules);
outErr.printOut(OptionsParser.newOptionsParser(options).getOptionsCompletion());
outErr.printOutLn("\"");
for (Map.Entry<String, BlazeCommand> e : commandsByName.entrySet()) {
BlazeCommand command = e.getValue();
String varName = e.getKey().toUpperCase(Locale.US).replace('-', '_');
Command annotation = command.getClass().getAnnotation(Command.class);
if (!annotation.completion().isEmpty()) {
outErr.printOutLn("BAZEL_COMMAND_" + varName + "_ARGUMENT=\"" + annotation.completion() + "\"");
}
options = BlazeCommandUtils.getOptions(command.getClass(), blazeModules, ruleClassProvider);
outErr.printOutLn("BAZEL_COMMAND_" + varName + "_FLAGS=\"");
outErr.printOut(OptionsParser.newOptionsParser(options).getOptionsCompletion());
outErr.printOutLn("\"");
}
}
Aggregations