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

Commit a301f59

Browse files
Add missing functions in fsi
1 parent e561b96 commit a301f59

File tree

2 files changed

+96
-34
lines changed

2 files changed

+96
-34
lines changed

src/Fargo/Fargo.fs

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,24 @@ module Fargo =
156156
let opt name alt value description: Arg<string option> =
157157
optc name alt value description (fun _ _ -> [])
158158

159+
let reqOpt (arg: Arg<'a option>) : Arg<'a> =
160+
{ Parse =
161+
fun tokens ->
162+
let result, rest, usages = arg.Parse tokens
163+
let reqResult =
164+
match result with
165+
| Ok (Some v) -> Ok v
166+
| Ok None ->
167+
let name =
168+
usages.Options
169+
|> List.tryHead
170+
|> Option.bind (fun u -> u.Name)
171+
|> Option.defaultValue "unknown"
172+
Error [$"Required argument %s{name} not found"]
173+
| Error e -> Error e
174+
reqResult, rest, Usage.req usages
175+
Complete = arg.Complete }
176+
159177

160178
let argc value description completer: Arg<string option> =
161179
let usage = { Name = None; Alt = None; Value = Some value; Description = description; Help = None; Type = UsageType.Arg}
@@ -196,24 +214,6 @@ module Fargo =
196214
Complete = arg.Complete
197215
}
198216

199-
let reqOpt (arg: Arg<'a option>) : Arg<'a> =
200-
{ Parse =
201-
fun tokens ->
202-
let result, rest, usages = arg.Parse tokens
203-
let reqResult =
204-
match result with
205-
| Ok (Some v) -> Ok v
206-
| Ok None ->
207-
let name =
208-
usages.Options
209-
|> List.tryHead
210-
|> Option.bind (fun u -> u.Name)
211-
|> Option.defaultValue "unknown"
212-
Error [$"Required argument %s{name} not found"]
213-
| Error e -> Error e
214-
reqResult, rest, Usage.req usages
215-
Complete = arg.Complete }
216-
217217
let flag name alt description : Arg<bool> =
218218
let usage = {Name = Some ("--" + name); Alt = Usage.Short.ofString alt; Value = None; Description = description; Help = None; Type = UsageType.Arg}
219219
let usages = { Path = []; Options = [usage]}
@@ -306,7 +306,7 @@ module Fargo =
306306
Complete = arg.Complete
307307
}
308308

309-
let all value description : Arg<Token list> =
309+
let all value description : Arg<Tokens> =
310310
{ Parse =
311311
fun tokens ->
312312
Ok tokens, [], { Path = []; Options = [{ Name = None; Alt = None; Value = Some value; Description = description; Help = None; Type = UsageType.Arg }] }
@@ -327,13 +327,7 @@ module Fargo =
327327

328328

329329
let nonEmpty error (arg: Arg<'a list>) : Arg<'a list> =
330-
{ Parse =
331-
fun tokens ->
332-
match arg.Parse tokens with
333-
| Ok [], rest, usage -> Error [error], rest, usage
334-
| Ok v, rest, usage -> Ok v, rest, usage
335-
| Error e, rest, usage -> Error e, rest, usage
336-
Complete = arg.Complete }
330+
validate (fun v -> not (List.isEmpty v)) error arg
337331

338332
let map (f: 'a -> 'b) (arg: Arg<'a>) : Arg<'b> =
339333
{ Parse =
@@ -665,8 +659,8 @@ module Run =
665659

666660
type Shell = Powershell
667661

668-
let printCompletion appName =
669-
function
662+
let printCompletion appName shell =
663+
match shell with
670664
| Powershell ->
671665
printfn """
672666
Register-ArgumentCompleter -Native -CommandName %s -ScriptBlock {
@@ -678,9 +672,6 @@ Register-ArgumentCompleter -Native -CommandName %s -ScriptBlock {
678672
""" appName appName
679673

680674

681-
let private (|Int|_|) (input: string) =
682-
Parsers.Int32.tryParse input
683-
684675
type TopCmd = CompleteCmd | CompletionCmd | RunCmd
685676
type Top =
686677
| TopComplete of position:int * cmdLine: Tokens

src/Fargo/Fargo.fsi

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,82 @@ module Usage =
4646
val complete: Usage -> string -> string list * bool
4747

4848

49+
val req: Usages -> Usages
50+
51+
module Short =
52+
val ofString : string -> string option
53+
54+
4955

5056

5157
[<AutoOpen>]
5258
module Fargo =
53-
/// parse the command line arguments with the specifier arg parser and pass
54-
/// parsed result to the suppied function
59+
/// A command parser. Commands have specified name or alt, and are always matched as the first token
5560
val cmd: name:string -> alt:string -> description:string -> Arg<string>
61+
62+
/// An option parser with a completer. Options are matched by name and are optional by default.
5663
val optc: name:string -> alt:string -> value: string -> description:string -> completer: Completer -> Arg<string option>
64+
/// An option parser. Options are specified by name and are optional by default.
5765
val opt: name:string -> alt:string -> value:string -> description:string -> Arg<string option>
5866

67+
/// Make an option required
5968
val reqOpt: arg:Arg<'a option> -> Arg<'a>
6069

70+
/// An argument parser with a completer. Arguments is position based and is optional by default
6171
val argc: value: string -> description:string -> completer: Completer -> Arg<string option>
72+
73+
/// An argument parser. Arguments is position based and is optional by default
6274
val arg: value: string -> description:string -> Arg<string option>
6375

76+
/// Make an argument required
6477
val reqArg: Arg<'a option> -> Arg<'a>
78+
79+
/// A flag parser. Flags are matched by name and are optional by default. Value is false when not specified
6580
val flag: string -> string -> string -> Arg<bool>
6681

82+
/// Make a flag required.
6783
val reqFlag: Arg<bool> -> Arg<bool>
6884

85+
/// Parse an arg value. If value parsing fails, the argument parsing stops with specified error message.
6986
val parse: ('a -> Result<'b, string>) -> Arg<'a> -> Arg<'b>
87+
88+
/// Parse an arg optional value. If value parsing fails, the argument parsing stops with specified error message.
7089
val optParse: ('a -> Result<'b, string>) -> Arg<'a option> -> Arg<'b option>
90+
91+
/// Parse all elements of a list argument. The parsing fails if any value parsing fails.
7192
val listParse: ('a -> Result<'b, string>) -> Arg<'a list> -> Arg<'b list>
7293

94+
/// An argument containing all the remaining tokens.
95+
val all: value:string -> description: string -> Arg<Tokens>
96+
97+
98+
/// Validates argument value. If validation fails, error message is used.
99+
val validate: f:('a -> bool) -> error:string -> arg:Arg<'a> -> Arg<'a>
100+
101+
/// Validates argument value. If validation fails, error message is used.
102+
val optValidate: f:('a -> bool) -> error:string -> arg: Arg<'a option> -> Arg<'a option>
103+
104+
/// Validates that the argument list value is not empty.
73105
val nonEmpty: string -> Arg<'a list> -> Arg<'a list>
74106

107+
/// Converts an argument value using specified function
75108
val map: ('a -> 'b) -> Arg<'a> -> Arg<'b>
109+
/// Converts an optional argument value using specified function
76110
val optMap: ('a -> 'b) -> Arg<'a option> -> Arg<'b option>
111+
112+
/// Use specified value for optional argument when argument is missing.
77113
val defaultValue: 'a -> Arg<'a option> -> Arg<'a>
114+
115+
/// Combines two arguments values using specified function.
78116
val map2: ('a -> 'b -> 'c) -> Arg<'a> -> Arg<'b> -> Arg<'c>
117+
118+
/// Returns new parser depending on an argument (usually a cmd) value.
79119
val bind: ('a -> Arg<'b>) -> Arg<'a> -> Arg<'b>
120+
121+
/// Creates an arguments that has the given value.
80122
val ret: 'a -> Arg<'a>
81123

124+
/// The builder for the fargo Computation Expression.
82125
[<Class>]
83126
type FargoBuilder =
84127
member Bind: Arg<'a> * ('a -> Arg<'b>) -> Arg<'b>
@@ -88,32 +131,60 @@ module Fargo =
88131
member ReturnFrom: Arg<'a> -> Arg<'a>
89132
member Zero: unit -> Arg<unit>
90133

134+
/// The fargo Computation Expression.
91135
val fargo: FargoBuilder
92136

93-
val alt: Arg<'a> -> Arg<'a> -> Arg<'a>
137+
/// Returns x value if parsing succeeds, otherwhite return y value.
138+
val alt: y:Arg<'a> -> x:Arg<'a> -> Arg<'a>
139+
/// Returns x value if parsing succeeds and has a value, otherwhite return y value.
94140
val optAlt: Arg<'a option> -> Arg<'a option> -> Arg<'a option>
141+
142+
/// Creates an argument that always returns given error message
95143
val error: message:string -> Arg<'a>
144+
145+
/// Creates an argument that always returns given error message
146+
val errors: messages:string list -> Arg<'a>
147+
148+
149+
150+
/// Creates an argument that returns a error message depending on remaining tokens.
96151
val errorf<'a> : messageFunc:(Token list -> string) -> Arg<'a>
97152

153+
/// Creates an arg that returns a error message indicating an unknown cmd.
98154
val cmdError<'a> : Arg<'a>
99155

156+
/// Add specific help message to specified argument.
100157
val help: text:string -> arg: Arg<'t> -> Arg<'t>
101158

102159
module Operators =
160+
/// Take value on the left, or the value on the right.
103161
val (<|>) : Arg<'a> -> Arg<'a> -> Arg<'a>
162+
/// Take value on the left, or the value on the right.
104163
val (<|?>) : Arg<'a option> -> Arg<'a option> -> Arg<'a option>
164+
/// Change the value of an argument with the specified constant.
105165
val (|>>) : Arg<'a> -> 'b -> Arg<'b>
106166

107167

108168
[<AutoOpen>]
109169
module Run =
170+
val toResult: arg: Arg<'a> -> Arg<ParseResult<'a> * Usages>
171+
110172
val tryParseTokens: arg:Arg<'a> -> tokens:Tokens -> Result<'a, string list * Usages>
111173
val complete: Arg<'a> -> int -> Tokens -> string list
112174

113175

176+
val printErrors: string seq -> unit
114177
val printUsage: Usages -> unit
178+
val printDescription: Usages -> unit
179+
val printOptions: Usage list -> unit
115180
val printHelp: Usages -> unit
116181

182+
type Shell = Powershell
183+
184+
val printCompletion: appName:string -> shell:Shell -> unit
185+
186+
val getUsages : arg:Arg<'a> -> Arg<Result<'a, string list> * Usages>
187+
117188
/// parse the command line arguments with the specifier arg parser and pass
118189
/// parsed result to the suppied function
119190
val run: appName:string -> arg:Arg<'a> -> cmdLine:string[] -> f:(Threading.CancellationToken -> 'a -> Threading.Tasks.Task<int>) -> int

0 commit comments

Comments
 (0)