Skip to content

Commit

Permalink
Correctly print CLI option aliases (#791)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer authored Feb 20, 2025
1 parent bdfaa9c commit bcbdb82
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 77 deletions.
16 changes: 7 additions & 9 deletions src-bootstrap/driver.spice
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,8 @@ p Driver.addInstallSubcommand() {
*/
p Driver.addUninstallSubcommand() {
// Create sub-command itself
CliSubcommand& subCmd = this.cliParser.addSubcommand("uninstall", "Builds your Spice program and runs it immediately");
CliSubcommand& subCmd = this.cliParser.addSubcommand("uninstall", "Uninstalls a Spice program from the system");
subCmd.addAlias("u");

this.addCompileSubcommandOptions(subCmd);
}

p Driver.addCompileSubcommandOptions(CliSubcommand& subCmd) {
Expand Down Expand Up @@ -344,12 +342,12 @@ p Driver.addCompileSubcommandOptions(CliSubcommand& subCmd) {
subCmd.addFlag("--use-lifetime-markers", this.cliOptions.useLifetimeMarkers, "Generate lifetime markers to enhance optimizations");

// Opt levels
subCmd.addOption("-O0", p(string& _v) { this.cliOptions.optLevel = OptLevel::O0; }, "Disable optimization for the output executable.");
subCmd.addOption("-O1", p(string& _v) { this.cliOptions.optLevel = OptLevel::O1; }, "Optimization level 1. Only basic optimization is executed.");
subCmd.addOption("-O2", p(string& _v) { this.cliOptions.optLevel = OptLevel::O2; }, "Optimization level 2. More advanced optimization is applied.");
subCmd.addOption("-O3", p(string& _v) { this.cliOptions.optLevel = OptLevel::O3; }, "Optimization level 3. Aggressive optimization for best performance.");
subCmd.addOption("-Os", p(string& _v) { this.cliOptions.optLevel = OptLevel::Os; }, "Optimization level s. Size optimization for output executable.");
subCmd.addOption("-Oz", p(string& _v) { this.cliOptions.optLevel = OptLevel::Oz; }, "Optimization level z. Aggressive optimization for best size.");
subCmd.addFlag("-O0", p(bool& _v) { this.cliOptions.optLevel = OptLevel::O0; }, "Disable optimization for the output executable.");
subCmd.addFlag("-O1", p(bool& _v) { this.cliOptions.optLevel = OptLevel::O1; }, "Optimization level 1. Only basic optimization is applied.");
subCmd.addFlag("-O2", p(bool& _v) { this.cliOptions.optLevel = OptLevel::O2; }, "Optimization level 2. More advanced optimization is applied.");
subCmd.addFlag("-O3", p(bool& _v) { this.cliOptions.optLevel = OptLevel::O3; }, "Optimization level 3. Aggressive optimization for best performance.");
subCmd.addFlag("-Os", p(bool& _v) { this.cliOptions.optLevel = OptLevel::Os; }, "Optimization level s. Size optimization for output executable.");
subCmd.addFlag("-Oz", p(bool& _v) { this.cliOptions.optLevel = OptLevel::Oz; }, "Optimization level z. Aggressive optimization for best size.");
subCmd.addFlag("-lto", this.cliOptions.useLTO, "Enable link time optimization (LTO)");

// --debug-output
Expand Down
10 changes: 5 additions & 5 deletions src/driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,7 @@ void Driver::addTestSubcommand() {
*/
void Driver::addInstallSubcommand() {
// Create sub-command itself
CLI::App *subCmd =
app.add_subcommand("install", "Builds your Spice program and installs it to a directory in the PATH variable");
CLI::App *subCmd = app.add_subcommand("install", "Builds your Spice program and installs it to a directory in the PATH");
subCmd->alias("i");
subCmd->ignore_case();
subCmd->callback([&] {
Expand All @@ -297,7 +296,7 @@ void Driver::addInstallSubcommand() {
*/
void Driver::addUninstallSubcommand() {
// Create sub-command itself
CLI::App *subCmd = app.add_subcommand("uninstall", "Builds your Spice program and runs it immediately");
CLI::App *subCmd = app.add_subcommand("uninstall", "Uninstalls a Spice program from the system");
subCmd->alias("u");
subCmd->ignore_case();
subCmd->callback([&] {
Expand Down Expand Up @@ -342,7 +341,7 @@ void Driver::addCompileSubcommandOptions(CLI::App *subCmd) {

// Opt levels
subCmd->add_flag_callback("-O0", [&] { cliOptions.optLevel = O0; }, "Disable optimization for the output executable.");
subCmd->add_flag_callback("-O1", [&] { cliOptions.optLevel = O1; }, "Only basic optimization is executed.");
subCmd->add_flag_callback("-O1", [&] { cliOptions.optLevel = O1; }, "Only basic optimization is applied.");
subCmd->add_flag_callback("-O2", [&] { cliOptions.optLevel = O2; }, "More advanced optimization is applied.");
subCmd->add_flag_callback("-O3", [&] { cliOptions.optLevel = O3; }, "Aggressive optimization for best performance.");
subCmd->add_flag_callback("-Os", [&] { cliOptions.optLevel = Os; }, "Size optimization for output executable.");
Expand All @@ -360,7 +359,8 @@ void Driver::addCompileSubcommandOptions(CLI::App *subCmd) {
// --dump-types
subCmd->add_flag<bool>("--dump-types,-types", cliOptions.dumpSettings.dumpTypes, "Dump all used types");
// --dump-cache-stats
subCmd->add_flag<bool>("--dump-cache-stats,-cache-stats", cliOptions.dumpSettings.dumpCacheStats, "Dump stats for compiler-internal lookup caches");
subCmd->add_flag<bool>("--dump-cache-stats,-cache-stats", cliOptions.dumpSettings.dumpCacheStats,
"Dump stats for compiler-internal lookup caches");
// --dump-ir
subCmd->add_flag<bool>("--dump-ir,-ir", cliOptions.dumpSettings.dumpIR, "Dump LLVM-IR");
// --dump-assembly
Expand Down
2 changes: 1 addition & 1 deletion src/symboltablebuilder/Scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Scope {
[[nodiscard]] bool doesAllowUnsafeOperations() const;
[[nodiscard]] bool isImportedBy(const Scope *askingScope) const;
[[nodiscard]] nlohmann::json getSymbolTableJSON() const;
ALWAYS_INLINE [[nodiscard]] bool isRootScope() const { return parent == nullptr; }
[[nodiscard]] ALWAYS_INLINE bool isRootScope() const { return parent == nullptr; }

// Wrapper methods for symbol table
ALWAYS_INLINE SymbolTableEntry *insert(const std::string &name, ASTNode *declNode) {
Expand Down
8 changes: 4 additions & 4 deletions src/typechecker/MacroDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
} \
}

#define HANDLE_UNRESOLVED_TYPE_QT(qualType) \
if ((qualType).is(TY_UNRESOLVED)) { \
#define HANDLE_UNRESOLVED_TYPE_QT(type) \
if ((type).is(TY_UNRESOLVED)) { \
if constexpr (std::is_base_of<ExprNode, decltype(node)>()) { \
const auto exprNode = spice_pointer_cast<ExprNode *>(node); \
return exprNode->setEvaluatedSymbolType(qualType, manIdx); \
return exprNode->setEvaluatedSymbolType(type, manIdx); \
} else { \
return qualType; \
return type; \
} \
}

Expand Down
38 changes: 36 additions & 2 deletions std/io/cli-option.spice
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import "std/os/os";
import "std/data/vector";
import "std/io/filepath";
import "std/text/print";
import "std/text/stringstream";

// Generic types
type T bool|string|String|int|long|short|FilePath;

// Enums
type CliOptionMode enum { SET_VALUE, CALL_CALLBACK }
type CliOptionMode enum {
SET_VALUE,
CALL_CALLBACK
}

public type CliOption<T> struct {
String name
Expand Down Expand Up @@ -62,4 +67,33 @@ public p CliOption.callCallback(T& value) {
p(T&) cb = this.callback;
cb(value);
}
}
}

public f<bool> CliOption.isPositional() {
if this.name.startsWith("-") {
return false;
}
foreach const String& optionAlias : this.aliases {
if optionAlias.startsWith("-") {
return false;
}
}
return true;
}

public p CliOption.printHelpItem() {
// Print name and aliases
StringStream ss;
ss << " " << this.name;
foreach const String& optionAlias : this.aliases {
ss << ',' << optionAlias;
}
// For all options, that are not bool flags, print an additional placeholder
if sizeof<T>() > 1 {
ss << " <value>";
}
printFixedWidth(ss.str(), 35, true);
// Description
printFixedWidth(this.description, 85, true);
lineBreak();
}
Loading

0 comments on commit bcbdb82

Please sign in to comment.