Treat --help, --license and --version as separate input modes

This commit is contained in:
Kamil Śliwak
2021-11-02 16:31:43 +01:00
parent a7b137829f
commit 93c1fe6878
6 changed files with 160 additions and 106 deletions
+24 -45
View File
@@ -16,8 +16,6 @@
*/
// SPDX-License-Identifier: GPL-3.0
#include "license.h"
#include <solc/CommandLineParser.h>
#include <libyul/optimiser/Suite.h>
#include <liblangutil/EVMVersion.h>
@@ -36,19 +34,12 @@ namespace po = boost::program_options;
namespace solidity::frontend
{
ostream& CommandLineParser::sout()
{
m_hasOutput = true;
return m_sout;
}
ostream& CommandLineParser::serr()
{
m_hasOutput = true;
return m_serr;
}
#define cout
#define cerr
static string const g_strAllowPaths = "allow-paths";
@@ -147,26 +138,6 @@ static map<InputMode, string> const g_inputModeName = {
{InputMode::Linker, "linker"},
};
void CommandLineParser::printVersionAndExit()
{
sout() <<
"solc, the solidity compiler commandline interface" <<
endl <<
"Version: " <<
solidity::frontend::VersionString <<
endl;
exit(EXIT_SUCCESS);
}
void CommandLineParser::printLicenseAndExit()
{
sout() << otherLicenses << endl;
// This is a static variable generated by cmake from LICENSE.txt
sout() << licenseText << endl;
exit(EXIT_SUCCESS);
}
bool CommandLineParser::checkMutuallyExclusive(vector<string> const& _optionNames)
{
if (countEnabledOptions(_optionNames) > 1)
@@ -297,11 +268,11 @@ OptimiserSettings CommandLineOptions::optimiserSettings() const
return settings;
}
bool CommandLineParser::parse(int _argc, char const* const* _argv, bool _interactiveTerminal)
bool CommandLineParser::parse(int _argc, char const* const* _argv)
{
m_hasOutput = false;
if (!parseArgs(_argc, _argv, _interactiveTerminal))
if (!parseArgs(_argc, _argv))
return false;
return processArgs();
@@ -482,6 +453,10 @@ bool CommandLineParser::parseOutputSelection()
switch (_mode)
{
case InputMode::Help:
case InputMode::License:
case InputMode::Version:
solAssert(false);
case InputMode::Compiler:
case InputMode::CompilerWithASTImport:
return contains(compilerModeOutputs, _outputName);
@@ -847,7 +822,7 @@ po::positional_options_description CommandLineParser::positionalOptionsDescripti
return filesPositions;
}
bool CommandLineParser::parseArgs(int _argc, char const* const* _argv, bool _interactiveTerminal)
bool CommandLineParser::parseArgs(int _argc, char const* const* _argv)
{
po::options_description allOptions = optionsDescription();
po::positional_options_description filesPositions = positionalOptionsDescription();
@@ -866,18 +841,6 @@ bool CommandLineParser::parseArgs(int _argc, char const* const* _argv, bool _int
return false;
}
if (m_args.count(g_strHelp) || (_interactiveTerminal && _argc == 1))
{
sout() << allOptions;
return false;
}
if (m_args.count(g_strVersion))
printVersionAndExit();
if (m_args.count(g_strLicense))
printLicenseAndExit();
po::notify(m_args);
return true;
@@ -886,6 +849,9 @@ bool CommandLineParser::parseArgs(int _argc, char const* const* _argv, bool _int
bool CommandLineParser::processArgs()
{
if (!checkMutuallyExclusive({
g_strHelp,
g_strLicense,
g_strVersion,
g_strStandardJSON,
g_strLink,
g_strAssemble,
@@ -895,7 +861,13 @@ bool CommandLineParser::processArgs()
}))
return false;
if (m_args.count(g_strStandardJSON) > 0)
if (m_args.count(g_strHelp) > 0)
m_options.input.mode = InputMode::Help;
else if (m_args.count(g_strLicense) > 0)
m_options.input.mode = InputMode::License;
else if (m_args.count(g_strVersion) > 0)
m_options.input.mode = InputMode::Version;
else if (m_args.count(g_strStandardJSON) > 0)
m_options.input.mode = InputMode::StandardJson;
else if (m_args.count(g_strAssemble) > 0 || m_args.count(g_strStrictAssembly) > 0 || m_args.count(g_strYul) > 0)
m_options.input.mode = InputMode::Assembler;
@@ -906,6 +878,13 @@ bool CommandLineParser::processArgs()
else
m_options.input.mode = InputMode::Compiler;
if (
m_options.input.mode == InputMode::Help ||
m_options.input.mode == InputMode::License ||
m_options.input.mode == InputMode::Version
)
return true;
map<string, set<InputMode>> validOptionInputModeCombinations = {
// TODO: This should eventually contain all options.
{g_strErrorRecovery, {InputMode::Compiler, InputMode::CompilerWithASTImport}},