Skip to content

Commit

Permalink
chore: Tidy up command line arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
vinniefalco committed Jun 12, 2023
1 parent f19a27a commit 5c90ac2
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 123 deletions.
24 changes: 12 additions & 12 deletions source/Tool/GenerateAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
// Official repository: https://github.com/cppalliance/mrdox
//

#include "Options.hpp"
#include "Tool/ConfigImpl.hpp"
#include "ToolArgs.hpp"
#include "ConfigImpl.hpp"
#include "CorpusImpl.hpp"
#include "AST/AbsoluteCompilationDatabase.hpp"
#include <mrdox/Generators.hpp>
Expand All @@ -32,23 +32,23 @@ DoGenerateAction()
std::string extraYaml;
{
llvm::raw_string_ostream os(extraYaml);
if(IgnoreMappingFailures.getValue())
if(toolArgs.ignoreMappingFailures.getValue())
os << "ignore-failures: true\n";
}

// Load configuration file
if(! ConfigPath.hasArgStr())
if(! toolArgs.configPath.hasArgStr())
return Error("the config path argument is missing");
auto config = loadConfigFile(ConfigPath, extraYaml);
auto config = loadConfigFile(toolArgs.configPath, extraYaml);
if(! config)
return config.getError();

// Load the compilation database
if(InputPaths.empty())
if(toolArgs.inputPaths.empty())
return Error("the compilation database path argument is missing");
if(InputPaths.size() > 1)
return Error("got {} input paths where 1 was expected", InputPaths.size());
auto compilationsPath = files::normalizePath(InputPaths.front());
if(toolArgs.inputPaths.size() > 1)
return Error("got {} input paths where 1 was expected", toolArgs.inputPaths.size());
auto compilationsPath = files::normalizePath(toolArgs.inputPaths.front());
std::string errorMessage;
auto jsonCompilations = tooling::JSONCompilationDatabase::loadFromFile(
compilationsPath, errorMessage, tooling::JSONCommandLineSyntax::AutoDetect);
Expand All @@ -70,9 +70,9 @@ DoGenerateAction()
auto ex = std::make_unique<tooling::AllTUsToolExecutor>(compilations, ThreadCount);

// Create the generator
auto generator = generators.find(FormatType.getValue());
auto generator = generators.find(toolArgs.formatType.getValue());
if(! generator)
return Error("the Generator \"{}\" was not found", FormatType.getValue());
return Error("the Generator \"{}\" was not found", toolArgs.formatType.getValue());

// Run the tool, this can take a while
auto corpus = CorpusImpl::build(*ex, *config);
Expand All @@ -82,7 +82,7 @@ DoGenerateAction()
// Run the generator.
if(config.get()->verboseOutput)
reportInfo("Generating docs...\n");
return generator->build(OutputPath.getValue(), **corpus);
return generator->build(toolArgs.outputPath.getValue(), **corpus);
}

} // mrdox
Expand Down
47 changes: 0 additions & 47 deletions source/Tool/Options.hpp

This file was deleted.

12 changes: 6 additions & 6 deletions source/Tool/TestAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
// Official repository: https://github.com/cppalliance/mrdox
//

#include "Options.hpp"
#include "SingleFileDB.hpp"
#include "Tool/ConfigImpl.hpp"
#include "ToolArgs.hpp"
#include "ConfigImpl.hpp"
#include "CorpusImpl.hpp"
#include "Support/Error.hpp"
#include <mrdox/Config.hpp>
Expand Down Expand Up @@ -214,7 +214,7 @@ handleFile(
return Error::success(); // keep going
}

if(ToolAction == Action::test)
if(toolArgs.toolAction == Action::test)
{
// Open and load XML comparison file
std::unique_ptr<llvm::MemoryBuffer> expectedXml;
Expand Down Expand Up @@ -252,7 +252,7 @@ handleFile(
results_.numberOfFailures++;
reportError("Test for \"{}\" failed", filePath);

if(badOption.getValue())
if(toolArgs.badOption.getValue())
{
// Write the .bad.xml file
auto bad = outputPath;
Expand Down Expand Up @@ -287,7 +287,7 @@ handleFile(
// success
}
}
else if(ToolAction == Action::update)
else if(toolArgs.toolAction == Action::update)
{
// Refresh the expected output file
if(auto err = writeFile(outputPath, generatedXml))
Expand Down Expand Up @@ -402,7 +402,7 @@ DoTestAction()
llvm::raw_string_ostream(extraYaml) <<
"concurrency: 1\n";
Results results;
for(auto const& inputPath : InputPaths)
for(auto const& inputPath : toolArgs.inputPaths)
{
TestRunner instance(results, extraYaml);
if(auto err = instance.checkPath(inputPath))
Expand Down
129 changes: 83 additions & 46 deletions source/Tool/Options.cpp → source/Tool/ToolArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,119 @@
// Official repository: https://github.com/cppalliance/mrdox
//

#include "Options.hpp"
#include "ToolArgs.hpp"
#include <cstddef>
#include <vector>

namespace clang {
namespace mrdox {

char const* Overview =
R"(Generate reference documentation, run tests against
a set of input vectors, or update a set of reference tests.)";

llvm::cl::OptionCategory Category("mrdox options");

llvm::cl::extrahelp ExtraHelp(
R"(Usage:
//------------------------------------------------

ToolArgs::
ToolArgs()
: genCat("Generation Options")
, testCat("Test Options")
, usageText(
R"( Generate C++ reference documentation
)")
, extraHelp(
R"(
USAGE:
mrdox .. ( compile-commands )
mrdox .. --action ( "test" | "update" ) ( dir | file )...
Examples
EXAMPLES:
mrdox --action test friend.cpp
mrdox --format adoc compile_commands.json
)");
)")

//
// Common options
//

llvm::cl::opt<Action> ToolAction(
, toolAction(
"action",
llvm::cl::desc(R"(Which action should be performed)"),
llvm::cl::init(Action::generate),
llvm::cl::values(
clEnumVal(test, "Compare output against expected"),
clEnumVal(update, "Update all expected xml files"),
clEnumVal(generate, "Generate reference documentation")),
llvm::cl::cat(Category));
clEnumVal(generate, "Generate reference documentation")))

// Test options
, configPath(
"config",
llvm::cl::desc(R"(The config filename relative to the repository root)"))

llvm::cl::opt<bool> badOption(
"bad",
llvm::cl::desc("Write a .bad.xml file for each test failure"),
llvm::cl::init(true),
llvm::cl::cat(Category));
, outputPath(
"output",
llvm::cl::desc("Directory or file for generating output."),
llvm::cl::init("."))

, inputPaths(
"inputs",
llvm::cl::Sink,
llvm::cl::desc("The path to the compilation database, or one or more .cpp files to test."))

//
// Generate options
//

llvm::cl::opt<std::string> FormatType(
, formatType(
"format",
llvm::cl::desc("Format for outputted docs (\"adoc\" or \"xml\")."),
llvm::cl::init("adoc"),
llvm::cl::cat(Category));
llvm::cl::cat(genCat))

// Common options

llvm::cl::opt<bool> IgnoreMappingFailures(
, ignoreMappingFailures(
"ignore-map-errors",
llvm::cl::desc("Continue if files are not mapped correctly."),
llvm::cl::init(true),
llvm::cl::cat(Category));

llvm::cl::opt<std::string> ConfigPath(
"config",
llvm::cl::desc(R"(The config filename relative to the repository root)"),
llvm::cl::init("mrdox.yml"),
llvm::cl::cat(Category));
llvm::cl::cat(genCat))

llvm::cl::opt<std::string> OutputPath(
"output",
llvm::cl::desc("Directory or file for generating output."),
llvm::cl::init("."),
llvm::cl::cat(Category));
//
// Test options
//

llvm::cl::list<std::string> InputPaths(
"inputs",
llvm::cl::Sink,
llvm::cl::desc("The path to the compilation database, or one or more .cpp files to test."),
llvm::cl::cat(Category));
, badOption(
"bad",
llvm::cl::desc("Write a .bad.xml file for each test failure"),
llvm::cl::init(true),
llvm::cl::cat(testCat))
{
}

ToolArgs ToolArgs::instance_;

void
ToolArgs::
hideForeignOptions()
{
// VFALCO When adding an option, it must
// also be added to this list or else it
// will stay hidden.

std::vector<llvm::cl::Option const*> ours({
&toolAction,
&formatType,
&configPath,
&outputPath,
std::addressof(inputPaths),
&ignoreMappingFailures,
&badOption
});

// Really hide the clang/llvm default
// options which we didn't ask for.
auto optionMap = llvm::cl::getRegisteredOptions();
for(auto& opt : optionMap)
{
if(std::find(ours.begin(), ours.end(), opt.getValue()) != ours.end())
opt.getValue()->setHiddenFlag(llvm::cl::NotHidden);
else
opt.getValue()->setHiddenFlag(llvm::cl::ReallyHidden);
}
}

} // mrdox
} // clang
69 changes: 69 additions & 0 deletions source/Tool/ToolArgs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Copyright (c) 2023 Vinnie Falco ([email protected])
//
// Official repository: https://github.com/cppalliance/mrdox
//

#ifndef MRDOX_TOOL_TOOLARGS_HPP
#define MRDOX_TOOL_TOOLARGS_HPP

#include <llvm/Support/CommandLine.h>
#include <string>

namespace clang {
namespace mrdox {

enum Action : int
{
test,
update,
generate
};

/** Command line options and tool settings.
*/
class ToolArgs
{
ToolArgs();

llvm::cl::OptionCategory genCat;
llvm::cl::OptionCategory testCat;

public:
static ToolArgs instance_;

char const* usageText;
llvm::cl::extrahelp extraHelp;

// Common options
llvm::cl::opt<Action> toolAction;
llvm::cl::opt<std::string> configPath;
llvm::cl::opt<std::string> outputPath;
llvm::cl::list<std::string> inputPaths;

// Generate options
llvm::cl::opt<std::string> formatType;
llvm::cl::opt<bool> ignoreMappingFailures;

// Test options
llvm::cl::opt<bool> badOption;

// Hide all options which don't belong to us
void hideForeignOptions();
};

/** Command line arguments passed to the tool.
This is a global variable because of how the
LLVM command line interface is designed.
*/
constexpr ToolArgs& toolArgs = ToolArgs::instance_;

} // mrdox
} // clang

#endif
Loading

0 comments on commit 5c90ac2

Please sign in to comment.