Skip to content
This repository was archived by the owner on Apr 5, 2025. It is now read-only.

Commit 8048bf7

Browse files
Improve documentation
1 parent 017d706 commit 8048bf7

File tree

1 file changed

+64
-32
lines changed

1 file changed

+64
-32
lines changed

readme.md

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,33 @@ Fargo features:
1616
Fargo is distributed as a nuget:
1717

1818
```pwsh
19-
dotnet add package Fargo
19+
dotnet add package Fargo.CmdLine
2020
```
2121

2222
In a fsx script:
2323
```fsharp
24-
#r "nuget: Fargo"
24+
#r "nuget: Fargo.CmdLine"
2525
```
2626

2727
You can try it with this simple hello world. Create a hello project:
2828

2929
```pwsh
3030
dotnet new console -lang F# -o ./hello
3131
cd ./hello
32-
dotnet add package Fargo
32+
dotnet add package Fargo.CmdLine
3333
```
3434

3535
and edit the `Program.fs` file:
3636

3737
```fsharp
3838
open Fargo
3939
40-
let parser = arg "text" "t" "The text to display" |> reqArg
40+
let parser = opt "text" "t" "text" "The text to display" |> reqOpt
4141
4242
[<EntryPoint>]
4343
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; })
4546
```
4647

4748
build the project
@@ -56,7 +57,7 @@ $env:PATH += ";$p"
5657
```
5758

5859
Run the program
59-
```psws
60+
```pwsh
6061
hello --text "Hello world!"
6162
```
6263

@@ -66,11 +67,9 @@ hello --help
6667
```
6768
will display:
6869
```
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
7473
```
7574

7675
If arguments are missing or incorrect, a detailed error is returned:
@@ -81,8 +80,9 @@ hello
8180
hello
8281
Required argument --text not found
8382
84-
Usage:
85-
--text, -t The text to display (required)
83+
Usage: --text <text>
84+
Arguments:
85+
--text, -t <text> The text to display
8686
```
8787

8888
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
123123

124124
## Arg
125125

126-
Arguments accept a value, and are optional by default:
126+
Arguments accept a positional value, and are optional by default:
127127

128128
```fsharp
129-
arg "value" "v" "the value" // Arg<string option>
129+
arg "value" "the value" // Arg<string option>
130130
```
131131

132+
Arguments are positional contrary to [options](#opt) which are named
133+
132134
It creates an `Arg<string option>` with a value of `Success(Some "a value")` containing the value when specified, and `Success None` otherwise.
133135

134136
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.
138140
To make an argument required, use the `reqArg` function:
139141

140142
```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>
142171
```
143172

144173
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
220249
A custom completer can be specified to enable completion on argument values:
221250

222251
```fsharp
223-
arg "value" "v" "the value"
224-
|> completer Completer.choices ["one"; "two"; "three" ]
252+
argc "value" "the value" (Completer.choices ["one"; "two"; "three" ])
253+
optc "name" "n" "value" "the name" (Completer.choices ["a"; "b"; "c" ])
225254
```
226255

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.
228257

229-
The function passed to the `completer` function must have the following signature:
258+
The function passed must have the following signature:
230259
```fsharp
231-
string -> string list
260+
string -> Token list -> string list
232261
```
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.
234263

235264
## Map2 and Applicatives
236265

@@ -315,7 +344,7 @@ The `error` function creates an `Arg<'t>` that always fails with specified error
315344
bind: ('a -> Arg<'b>) -> Arg<'a> -> Arg<'b>
316345
```
317346

318-
The use of `bind` directly is discouraged, prefere the computation expression:
347+
The use of `bind` directly is discouraged, prefer the computation expression:
319348

320349
```fsharp
321350
type FileCmd = Load | Save
@@ -327,10 +356,10 @@ cmdLine {
327356
<|> (cmd "save" "sv" "saves the document" |~> FileCmd.Save)
328357
<|> (error "Invalid file command") with
329358
| FileCmd.Load ->
330-
let! path = arg "path" "p" "the path"
359+
let! path = opt "path" "p" "path" "the path"
331360
return Load path
332361
| FileCmd.Load ->
333-
let! path = arg "path" "p" "the path"
362+
let! path = opt "path" "p" "path" "the path"
334363
and! force = flag "force" "f" "overwrite file"
335364
return Save(path, force)
336365
} // Arg<Command>
@@ -347,10 +376,10 @@ cmdLine {
347376
match! cmd "load" "ld" "loads the document"
348377
<|> cmd "save" "sv" "saves the document" with
349378
| "load" ->
350-
let! path = arg "path" "p" "the path"
379+
let! path = opt "path" "p" "path" "the path"
351380
return Load path
352381
| "save" ->
353-
let! path = arg "path" "p" "the path"
382+
let! path = opt "path" "p" "path" "the path"
354383
and! force = flag "force" "f" "overwrite file"
355384
return Save(path, force)
356385
| _ -> return error "Unknown command"
@@ -367,14 +396,14 @@ cmdLine {
367396
match! cmd "load" "ld" "loads the document"
368397
<|> cmd "save" "sv" "saves the document" with
369398
| "load" ->
370-
let! path = arg "path" "p" "the path"
399+
let! path = opt "path" "p" "path" "the path"
371400
return Load path
372401
| "save" ->
373-
let! path = arg "path" "p" "the path"
402+
let! path = opt "path" "p" "path" "the path"
374403
and! force = flag "force" "f" "overwrite file"
375404
return Save(path, force)
376405
| _ ->
377-
let! path = arg "path" "p" "the path"
406+
let! path = opt "path" "p" "path" "the path"
378407
return Touch path
379408
} // Arg<Command>
380409
```
@@ -439,8 +468,11 @@ let p =
439468
440469
[<EntryPoint>]
441470
let main (args: string[]) =
442-
run "myapp" p args (fun cmd ->
443-
// excution of match commands here...
471+
run "myapp" p args (fun ct cmd ->
472+
task {
473+
// excution of match commands here...
474+
return 0
475+
}
444476
)
445477
```
446478

0 commit comments

Comments
 (0)