Skip to content

Commit

Permalink
allow -path before or after subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
yonaskolb committed Jan 6, 2018
1 parent ef5e025 commit 53a5610
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 2 additions & 3 deletions Sources/BeakCore/Beak.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Beak {

let parser = ArgumentParser(commandName: "beak", usage: "[--path] [subcommand]", overview: "Beak can inspect and run functions in your swift scripts")
let versionArgument = parser.add(option: "--version", shortName: "-v", kind: Bool.self, usage: "Prints the current version of Beak")
let pathArgument = parser.add(option: "--path", shortName: "-p", kind: String.self, usage: "The path to a swift file. Defaults to beak.swift", completion: .filename)
_ = parser.add(option: "--path", shortName: "-p", kind: String.self, usage: "The path to a swift file. Defaults to beak.swift", completion: .filename)

let commands = [
"list": ListCommand(options: options, parentParser: parser),
Expand All @@ -47,8 +47,7 @@ public class Beak {

if let subParser = parsedArguments.subparser(parser),
let command = commands[subParser] {
let path = Path(parsedArguments.get(pathArgument) ?? "beak.swift").normalize()
try command.execute(parsedArguments: parsedArguments, path: path)
try command.execute(parsedArguments: parsedArguments)
} else {
parser.printUsage(on: stdoutStream)
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/BeakCore/Commands/BeakCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ class BeakCommand {

let parser: ArgumentParser
let options: BeakOptions
let pathArgument: OptionArgument<String>

init(options: BeakOptions, parentParser: ArgumentParser, name: String, description: String) {
self.options = options
parser = parentParser.add(subparser: name, overview: description)
pathArgument = parser.add(option: "--path", shortName: "-p", kind: String.self, usage: "The path to a swift file. Defaults to beak.swift", completion: .filename)
}

func execute(parsedArguments: ArgumentParser.Result, path: Path) throws {
func execute(parsedArguments: ArgumentParser.Result) throws {
let path = Path(parsedArguments.get(pathArgument) ?? "beak.swift").normalize()
let beakFile = try BeakFile(path: path)
try execute(path: path, beakFile: beakFile, parsedArguments: parsedArguments)
}
Expand Down

0 comments on commit 53a5610

Please sign in to comment.