This commit is contained in:
Matthew Bessette 2025-03-15 20:56:50 -04:00
commit 3629cb7243
6 changed files with 102 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
bin
!bin/.githold

7
Makefile Normal file
View File

@ -0,0 +1,7 @@
build:
amber build src/index.amber bin/some-cli
clean:
rm -rf bin
mkdir bin
touch bin/.githold

View File

@ -0,0 +1,10 @@
import { bold, echo_info, underlined } from "std/env"
pub fun help(): Null {
const usage = underlined(bold("Usage"))
const commands = underlined(bold("Commands"))
echo_info("{usage}: some-cli <command> [--flag[=value]] [-f[=value]]")
echo_info("")
echo_info("{commands}")
echo_info(" help Print this message or the help of the given command")
}

View File

@ -0,0 +1,55 @@
import { array_find, array_find_all, array_remove_at } from "std/array"
import { slice, split_chars, starts_with, text_contains } from "std/text"
fun remove_prefix(arg: Text): Text? {
if starts_with(arg, "--") {
return slice(arg, 2)
}
if starts_with(arg, "-") {
return slice(arg, 1)
}
fail 2 // "Flags must start with `--` or `-` given {arg}"
}
fun safe_split_flag(arg: Text): [Text] {
const string_of_chars = split_chars(arg)
const first_equals = array_find(string_of_chars, "=")
if first_equals == -1 {
return [arg, ""]
}
const flag = slice(arg, 0, first_equals)
const value = slice(arg, first_equals+1)
echo "{flag} {value}"
return [flag, value]
}
// Returns associative map of flagName -> value or flagName -> "" if just present
pub fun arg_parser(arguments: [Text]): [Text]? {
if len(arguments) < 3 {
return [Text]
}
let results = [Text]
for i in 2..len(arguments) {
const arg = remove_prefix(arguments[i]) failed {
fail status
}
if text_contains(arg, "=") {
results = results + safe_split_flag(arg)
continue
}
results = results + [arg, ""]
}
return results
}

View File

@ -0,0 +1,8 @@
pub fun resolve_status_code(code: Num): Text {
if {
code == 0: return ""
code == 1: return "Unknown error..."
code == 2: return "Flags must start with `--` or `-`"
else: return "Unknown error... {code}"
}
}

20
src/index.amber Normal file
View File

@ -0,0 +1,20 @@
import { arg_parser } from "./helpers/arg-parser.amber"
import { resolve_status_code } from "./helpers/resolve-status-code.amber"
import { help } from "./command-hooks/help.amber"
import { echo_error } from "std/env"
main(cli_params) {
const command = cli_params[1]
const flags = arg_parser(cli_params) failed {
echo_error(resolve_status_code(status))
fail 1
}
echo "We parsed: command={command} flags={flags}"
if {
command == "help": help()
else: help()
}
}