You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 5, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: readme.md
+64-32Lines changed: 64 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,32 +16,33 @@ Fargo features:
16
16
Fargo is distributed as a nuget:
17
17
18
18
```pwsh
19
-
dotnet add package Fargo
19
+
dotnet add package Fargo.CmdLine
20
20
```
21
21
22
22
In a fsx script:
23
23
```fsharp
24
-
#r "nuget: Fargo"
24
+
#r "nuget: Fargo.CmdLine"
25
25
```
26
26
27
27
You can try it with this simple hello world. Create a hello project:
28
28
29
29
```pwsh
30
30
dotnet new console -lang F# -o ./hello
31
31
cd ./hello
32
-
dotnet add package Fargo
32
+
dotnet add package Fargo.CmdLine
33
33
```
34
34
35
35
and edit the `Program.fs` file:
36
36
37
37
```fsharp
38
38
open Fargo
39
39
40
-
let parser = arg "text" "t" "The text to display" |> reqArg
40
+
let parser = opt "text" "t" "text" "The text to display" |> reqOpt
41
41
42
42
[<EntryPoint>]
43
43
let main args =
44
-
run "hello" parser args (fun text -> printfn "%s" text)
44
+
run "hello" parser args (fun ct text ->
45
+
task { printfn "%s" text; return 0; })
45
46
```
46
47
47
48
build the project
@@ -56,7 +57,7 @@ $env:PATH += ";$p"
56
57
```
57
58
58
59
Run the program
59
-
```psws
60
+
```pwsh
60
61
hello --text "Hello world!"
61
62
```
62
63
@@ -66,11 +67,9 @@ hello --help
66
67
```
67
68
will display:
68
69
```
69
-
hello
70
-
Required argument --text not found
71
-
72
-
Usage:
73
-
--text, -t The text to display (required)
70
+
Usage: --text <text>
71
+
Arguments:
72
+
--text, -t <text> The text to display
74
73
```
75
74
76
75
If arguments are missing or incorrect, a detailed error is returned:
@@ -81,8 +80,9 @@ hello
81
80
hello
82
81
Required argument --text not found
83
82
84
-
Usage:
85
-
--text, -t The text to display (required)
83
+
Usage: --text <text>
84
+
Arguments:
85
+
--text, -t <text> The text to display
86
86
```
87
87
88
88
To enable completion in powershell, execute the following line:
@@ -123,12 +123,14 @@ It creates an `Arg<bool>` with a value of `Success true` when specified and a va
123
123
124
124
## Arg
125
125
126
-
Arguments accept a value, and are optional by default:
126
+
Arguments accept a positional value, and are optional by default:
127
127
128
128
```fsharp
129
-
arg "value" "v" "the value" // Arg<string option>
129
+
arg "value" "the value" // Arg<string option>
130
130
```
131
131
132
+
Arguments are positional contrary to [options](#opt) which are named
133
+
132
134
It creates an `Arg<string option>` with a value of `Success(Some "a value")` containing the value when specified, and `Success None` otherwise.
133
135
134
136
When the second argument is `null` the flag has no short alternate version.
@@ -138,7 +140,34 @@ When the second argument is `null` the flag has no short alternate version.
138
140
To make an argument required, use the `reqArg` function:
139
141
140
142
```fsharp
141
-
arg "value" "v" "the value" |> reqArg |> Arg<string>
143
+
arg "value" "the value" |> reqArg |> Arg<string>
144
+
```
145
+
146
+
It creates an `Arg<string>` with a value of `Success "a value"` containing the value when specified, and `Failure ["Require argument --value not found"]` otherwise.
147
+
148
+
The first parameter is used as a placeholder in the usage syntax.
149
+
150
+
151
+
## Opt
152
+
153
+
Options accept a named value, and are optional by default:
154
+
155
+
```fsharp
156
+
opt "name" "n" "the-name" "the name" // Arg<string option>
157
+
```
158
+
159
+
It creates an `Arg<string option>` with a value of `Success(Some "a value")` containing the value when specified, and `Success None` otherwise.
160
+
161
+
When the second argument is `null` the flag has no short alternate version.
162
+
163
+
The third parameter is used as a placeholder in the usage syntax.
164
+
165
+
## ReqArg
166
+
167
+
To make an argument required, use the `reqArg` function:
168
+
169
+
```fsharp
170
+
opt "name" "n" "the-name" "the name" |> reqOpt |> Arg<string>
142
171
```
143
172
144
173
It creates an `Arg<string>` with a value of `Success "a value"` containing the value when specified, and `Failure ["Require argument --value not found"]` otherwise.
@@ -220,17 +249,17 @@ In this example, when the argument is not specified, the value will be 0. When s
220
249
A custom completer can be specified to enable completion on argument values:
argc "value" "the value" (Completer.choices ["one"; "two"; "three" ])
253
+
optc "name" "n" "value" "the name" (Completer.choices ["a"; "b"; "c" ])
225
254
```
226
255
227
-
Pressing the `tab` key after `--value` will suggest one of the specified values.
256
+
Pressing the `tab` key after `--name` will suggest one of the specified values.
228
257
229
-
The function passed to the `completer` function must have the following signature:
258
+
The function passed must have the following signature:
230
259
```fsharp
231
-
string -> string list
260
+
string -> Token list -> string list
232
261
```
233
-
The input string is the text of the argument value when completion is requested. The function should return a list of suggested values as strings.
262
+
The input string is the text of the argument value when completion is requested. The token list contains all tokens that have not been parsed yet. The function should return a list of suggested values as strings.
234
263
235
264
## Map2 and Applicatives
236
265
@@ -315,7 +344,7 @@ The `error` function creates an `Arg<'t>` that always fails with specified error
315
344
bind: ('a -> Arg<'b>) -> Arg<'a> -> Arg<'b>
316
345
```
317
346
318
-
The use of `bind` directly is discouraged, prefere the computation expression:
347
+
The use of `bind` directly is discouraged, prefer the computation expression:
319
348
320
349
```fsharp
321
350
type FileCmd = Load | Save
@@ -327,10 +356,10 @@ cmdLine {
327
356
<|> (cmd "save" "sv" "saves the document" |~> FileCmd.Save)
0 commit comments