cli: Initial _real_ Cli Work

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-11 08:56:38 -04:00
parent 73b4f54bbe
commit fccaf1aa92
2 changed files with 56 additions and 6 deletions
+55 -5
View File
@@ -17,13 +17,63 @@
import ArgumentParser import ArgumentParser
import Common import Common
import P4Compiler
import SystemPackage
@main @main
struct Cli: ParsableCommand { struct Cli: ParsableCommand {
public func run() throws { @Flag(help: "Disable ANSI-stylized output.") var plain: Int
let formatter = FormatterPlain()
let e = ErrorWithLocation(sourceLocation: SourceLocation(1, 5), withError: "Testing") static let configuration = CommandConfiguration(
let e1 = ErrorWithLocation(sourceLocation: SourceLocation(10, 5), withError: "Oh no") abstract: "P4CE compiler, interpreter and debugger.",
print(e.append(error: e1).format(formatter)) subcommands: [Compile.self])
}
struct CliOptions: ParsableArguments {
@ArgumentParser.Argument(help: "File to compile.") // Have to be explicit because Common has an Argument, too!
var path: String
@Option(name: [.customShort("I")], help: "Search paths.")
var search: [String] = []
}
extension Cli {
struct Compile: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Compile P4CE code.")
@ParentCommand var parent: Cli
@OptionGroup var options: CliOptions
mutating func run() {
let sm = SourceManager(options.search.map { FilePath($0) })
let prep = SourceCodePreprocessor(sm)
let file = FilePath(options.path)
let maybe_source = prep.preprocess(file)
guard case .Ok(let source) = maybe_source else {
let formatter = FormatterAnsi()
print(ErrorWithLabel("Preprocessor Error", maybe_source.error()!).format(formatter))
return
}
let maybe_program = Program.Compile(source.getSource())
guard case .Ok(_) = maybe_program else {
let formatter = FormatterAnsi()
print(ErrorWithLabel("Compiler Error", maybe_source.error()!).format(formatter))
return
}
let success_formatter: any Formattable =
if parent.plain != 0 {
FormatterPlain()
} else {
FormatterAnsi()
}
print(
success_formatter.formatWithStyle(
"Success", Style(StyleColor.Green, [StyleFormat.Underline])))
}
} }
} }
+1 -1
View File
@@ -107,7 +107,7 @@ public struct ErrorWithLabel: Errorable {
public func format(_ formatter: any Formattable) -> String { public func format(_ formatter: any Formattable) -> String {
let green = Style(StyleColor.Green) let green = Style(StyleColor.Green)
let formatted_label = formatter.formatWithStyle(self.label, green) let formatted_label = formatter.formatWithStyle(self.label, green)
return formatted_label + self.error.format(formatter) return formatted_label + ": " + self.error.format(formatter)
} }
public init(_ label: String, _ error: any Errorable) { public init(_ label: String, _ error: any Errorable) {