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
+57 -9
View File
@@ -23,6 +23,7 @@
*/
#include <solc/CommandLineInterface.h>
#include "license.h"
#include "solidity/BuildInfo.h"
#include <libsolidity/interface/Version.h>
@@ -405,6 +406,13 @@ bool CommandLineInterface::readInputFiles()
{
solAssert(!m_standardJsonInput.has_value(), "");
if (
m_options.input.mode == InputMode::Help ||
m_options.input.mode == InputMode::License ||
m_options.input.mode == InputMode::Version
)
return true;
m_fileReader.setBasePath(m_options.input.basePath);
if (m_fileReader.basePath() != "")
@@ -573,8 +581,18 @@ void CommandLineInterface::createJson(string const& _fileName, string const& _js
bool CommandLineInterface::parseArguments(int _argc, char const* const* _argv)
{
CommandLineParser parser(sout(/* _markAsUsed */ false), serr(/* _markAsUsed */ false));
bool success = parser.parse(_argc, _argv, isatty(fileno(stdin)));
CommandLineParser parser(serr(/* _markAsUsed */ false));
if (isatty(fileno(stdin)) && _argc == 1)
{
// If the terminal is taking input from the user, provide more user-friendly output.
CommandLineParser::printHelp(sout());
// In this case we want to exit with an error but not display any error message.
return false;
}
bool success = parser.parse(_argc, _argv);
if (!success)
return false;
m_hasOutput = m_hasOutput || parser.hasOutput();
@@ -587,6 +605,15 @@ bool CommandLineInterface::processInput()
{
switch (m_options.input.mode)
{
case InputMode::Help:
CommandLineParser::printHelp(sout());
return false;
case InputMode::License:
printLicense();
return true;
case InputMode::Version:
printVersion();
return true;
case InputMode::StandardJson:
{
solAssert(m_standardJsonInput.has_value(), "");
@@ -611,6 +638,19 @@ bool CommandLineInterface::processInput()
return false;
}
void CommandLineInterface::printVersion()
{
sout() << "solc, the solidity compiler commandline interface" << endl;
sout() << "Version: " << solidity::frontend::VersionString << endl;
}
void CommandLineInterface::printLicense()
{
sout() << otherLicenses << endl;
// This is a static variable generated by cmake from LICENSE.txt
sout() << licenseText << endl;
}
bool CommandLineInterface::compile()
{
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
@@ -845,16 +885,24 @@ void CommandLineInterface::handleAst()
bool CommandLineInterface::actOnInput()
{
if (m_options.input.mode == InputMode::StandardJson || m_options.input.mode == InputMode::Assembler)
// Already done in "processInput" phase.
return true;
else if (m_options.input.mode == InputMode::Linker)
writeLinkedFiles();
else
switch (m_options.input.mode)
{
solAssert(m_options.input.mode == InputMode::Compiler || m_options.input.mode == InputMode::CompilerWithASTImport, "");
case InputMode::Help:
case InputMode::License:
case InputMode::Version:
case InputMode::StandardJson:
case InputMode::Assembler:
// Already done in "processInput" phase.
break;
case InputMode::Linker:
writeLinkedFiles();
break;
case InputMode::Compiler:
case InputMode::CompilerWithASTImport:
outputCompilationResults();
break;
}
return !m_outputFailed;
}