-
Notifications
You must be signed in to change notification settings - Fork 1
/
outil.gleam
104 lines (93 loc) · 2.7 KB
/
outil.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import gleam/io
import gleam/option.{type Option}
import outil/error.{type Reason}
/// Create a command with the given name and description, and pass it to the
/// given continuation function for further configuration.
///
/// The command gets a default implementation that returns an error.
pub fn command(
name: String,
description: String,
argv: List(String),
continue: fn(Command) -> a,
) -> a {
continue(Command(name, description, [], [], argv))
}
/// Convenience function for handling the result of executing a command.
///
/// Print usage if there was a command line error or if the user asked for it.
///
/// Does nothing if the command returned an application error, assuming that
/// the application will handle it.
pub fn print_usage(return) {
case return {
CommandLineError(_, usage) -> io.println(usage)
Help(usage) -> io.println(usage)
_ -> Nil
}
}
/// Convenience function for handling the result of executing a command.
///
/// Print usage if there was a command line error or if the user asked for it.
///
/// Exit with 2 if there was a command line error, 1 if there was a command
/// error, or 0 if the command was successful.
pub fn print_usage_and_exit(return) {
print_usage(return)
case return {
CommandLineError(_, _) -> halt_program(2)
CommandError(_) -> halt_program(1)
Help(_) -> halt_program(0)
}
}
/// A command line interface to a run function.
pub type Command {
Command(
name: String,
description: String,
arguments: List(Argument),
options: List(Opt),
argv: List(String),
)
}
/// A positional command line argument.
pub type Argument {
BoolArgument(name: String)
FloatArgument(name: String)
IntArgument(name: String)
StringArgument(name: String)
}
/// A command line option/flag.
pub type Opt {
Opt(long: String, short: Option(String), description: String, value: OptValue)
}
/// The type and default value of an option.
pub type OptValue {
BoolOpt
FloatOpt(default: Float)
IntOpt(default: Int)
StringOpt(default: String)
}
/// Non-normal return values from executing a command.
pub type CommandReturn(a) {
/// An error in parsing the command line.
CommandLineError(reason: Reason, usage: String)
/// An error in executing the command, `a` is your error value.
CommandError(a)
/// The user asked for help.
Help(usage: String)
}
/// The result of executing a command.
pub type CommandResult(a, b) =
Result(a, CommandReturn(b))
/// Parse a Bool from a string.
pub fn parse_bool(arg: String) -> Result(Bool, Nil) {
case arg {
"true" -> Ok(True)
"false" -> Ok(False)
_ -> Error(Nil)
}
}
@external(erlang, "erlang", "halt")
@external(javascript, "./outil_ffi.mjs", "exit")
fn halt_program(code: Int) -> Nil