Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed Feb 11, 2024
1 parent 8c2aa99 commit ebde6ab
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/glint.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import gleam_community/ansi
import gleam_community/colour.{type Colour}
import gleam/result
import gleam/function
import gleam

// --- CONFIGURATION ---

Expand Down Expand Up @@ -94,10 +95,10 @@ pub opaque type Glint(a) {
///
pub type ArgsCount {
/// Specifies that a command must accept a specific number of arguments
///
///
EqArgs(Int)
/// Specifies that a command must accept a minimum number of arguments
///
///
MinArgs(Int)
}

Expand All @@ -118,10 +119,10 @@ pub opaque type Command(a) {
/// Arguments passed to `glint` are provided as the `args` field.
///
/// Flags passed to `glint` are provided as the `flags` field.
///
///
/// If named arguments are specified at command creation, they will be accessible via the `named_args` field.
/// IMPORTANT: Arguments matched by `named_args` will not be present in the `args` field.
///
///
pub type CommandInput {
CommandInput(
args: List(String),
Expand Down Expand Up @@ -156,7 +157,7 @@ pub type Out(a) {
/// Result type for command execution
///
pub type CmdResult(a) =
Result(Out(a))
gleam.Result(Out(a), #(snag.Snag, String))

// -- CORE: BUILDER FUNCTIONS --

Expand Down Expand Up @@ -252,7 +253,7 @@ pub fn count_args(cmd: Command(a), count: ArgsCount) -> Command(a) {
/// These named arguments will be matched with the first N arguments passed to the command
/// All named arguments must match for a command to succeed, this is considered an implicit MinArgs(N)
/// This works in combination with CommandInput.named_args which will contain the matched args in a Dict(String,String)
/// IMPORTANT: Matched named arguments will not be present in CommandInput.args
/// IMPORTANT: Matched named arguments will not be present in CommandInput.args
///
pub fn named_args(cmd: Command(a), args: List(String)) -> Command(a) {
Command(..cmd, named_args: args)
Expand Down Expand Up @@ -376,7 +377,7 @@ fn do_execute(

// when there are no more available arguments
// run the current command
[] -> execute_root(cmd, global_flags, [], flags)
[] -> execute_root(command_path, config, cmd, global_flags, [], flags)

// when there are arguments remaining
// check if the next one is a subcommand of the current command
Expand All @@ -397,7 +398,7 @@ fn do_execute(
|> Ok
// subcommand not found, but help flag has not been passed
// execute the current command
_ -> execute_root(cmd, global_flags, args, flags)
_ -> execute_root(command_path, config, cmd, global_flags, args, flags)
}
}
}
Expand All @@ -419,6 +420,8 @@ fn args_compare(expected: ArgsCount, actual: Int) -> Result(Nil) {
/// Executes the current root command.
///
fn execute_root(
path: List(String),
config: Config,
cmd: CommandNode(a),
global_flags: FlagMap,
args: List(String),
Expand Down Expand Up @@ -453,6 +456,9 @@ fn execute_root(
}
|> option.unwrap(snag.error("command not found"))
|> snag.context("failed to run command")
|> result.map_error(fn(err) {
#(err, cmd_help(path, cmd, config, global_flags))
})
}

/// A wrapper for `execute` that prints any errors enountered or the help text if requested.
Expand All @@ -472,10 +478,14 @@ pub fn run_and_handle(
with handle: fn(a) -> _,
) -> Nil {
case execute(glint, args) {
Error(err) ->
err
|> snag.pretty_print
|> io.println
Error(#(snerr, help)) -> {
io.println(
snag.pretty_print(snerr)
<> "\n\nSee the help text as follows:\n\n"
<> help,
)
Nil
}
Ok(Help(help)) -> io.println(help)
Ok(Out(out)) -> {
handle(out)
Expand Down
18 changes: 18 additions & 0 deletions test/glint_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ pub fn help_test() {
glint.execute(cli, ["a", "b"])
|> should.equal(Ok(Out(Nil)))

glint.execute(cli, ["a"])
|> should.be_error()

glint.execute(cli, [])
|> should.be_error()

glint.execute(cli, ["cmd2"])
|> should.be_error()

glint.execute(cli, ["cmd2", "1"])
|> should.be_error()

glint.execute(cli, ["cmd2", "1", "2"])
|> should.equal(Ok(Out(Nil)))

glint.execute(cli, ["cmd2", "1", "2", "3"])
|> should.equal(Ok(Out(Nil)))

// help message for root command
glint.execute(cli, [glint.help_flag()])
|> should.equal(
Expand Down

0 comments on commit ebde6ab

Please sign in to comment.