Skip to content

Commit 02aa1f3

Browse files
rename description --> helpTextMessage
1 parent 16f077c commit 02aa1f3

5 files changed

Lines changed: 65 additions & 67 deletions

File tree

src/Argu/ArgumentParser.fs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ open FSharp.Reflection
1111
/// that is an F# discriminated union. It can then be used to parse command line arguments
1212
/// or XML configuration.
1313
[<AbstractClass; NoEquality; NoComparison; AutoSerializable(false)>]
14-
type ArgumentParser internal (argInfo : UnionArgInfo, _programName : string, description : string option,
14+
type ArgumentParser internal (argInfo : UnionArgInfo, _programName : string, helpTextMessage : string option,
1515
_usageStringCharacterWidth : int, errorHandler : IExiter) =
1616

1717
/// Gets the help flags specified for the CLI parser
1818
member __.HelpFlags = argInfo.HelpParam.Flags
1919
/// Gets the help description specified for the CLI parser
2020
member __.HelpDescription = argInfo.HelpParam.Description
21+
/// Gets the message that will be displayed at the top of the help text
22+
member __.HelpTextMessage = helpTextMessage
2123
/// Returns true if parser corresponds to a subcommand
2224
member __.IsSubCommandParser = argInfo.TryGetParent() |> Option.isSome
2325
/// If subcommand parser, gets parent argument metadata
@@ -39,7 +41,7 @@ type ArgumentParser internal (argInfo : UnionArgInfo, _programName : string, des
3941
e.Accept {
4042
new ITemplateFunc<ArgumentParser> with
4143
member __.Invoke<'Template when 'Template :> IArgParserTemplate> () =
42-
new ArgumentParser<'Template>(nu, _programName, description, _usageStringCharacterWidth, errorHandler) :> _
44+
new ArgumentParser<'Template>(nu, _programName, helpTextMessage, _usageStringCharacterWidth, errorHandler) :> _
4345
})
4446
|> Seq.toList
4547

@@ -50,7 +52,7 @@ type ArgumentParser internal (argInfo : UnionArgInfo, _programName : string, des
5052
member __.PrintUsage (?message : string, ?programName : string, ?usageStringCharacterWidth : int) : string =
5153
let programName = defaultArg programName _programName
5254
let usageStringCharacterWidth = defaultArg usageStringCharacterWidth _usageStringCharacterWidth
53-
printUsage argInfo programName usageStringCharacterWidth message |> StringExpr.build
55+
mkUsageString argInfo programName usageStringCharacterWidth message |> StringExpr.build
5456

5557
/// <summary>
5658
/// Prints command line syntax. Useful for generating documentation.
@@ -60,7 +62,7 @@ type ArgumentParser internal (argInfo : UnionArgInfo, _programName : string, des
6062
member __.PrintCommandLineSyntax (?programName : string, ?usageStringCharacterWidth : int) : string =
6163
let programName = defaultArg programName _programName
6264
let usageStringCharacterWidth = defaultArg usageStringCharacterWidth _usageStringCharacterWidth
63-
printCommandLineSyntax argInfo "" usageStringCharacterWidth programName |> StringExpr.build
65+
mkCommandLineSyntax argInfo "" usageStringCharacterWidth programName |> StringExpr.build
6466

6567
/// <summary>
6668
/// Enables access to the typed API of an ArgumentParser
@@ -74,36 +76,36 @@ type ArgumentParser internal (argInfo : UnionArgInfo, _programName : string, des
7476
/// or XML configuration.
7577
and [<Sealed; NoEquality; NoComparison; AutoSerializable(false)>]
7678
ArgumentParser<'Template when 'Template :> IArgParserTemplate>
77-
internal (argInfo : UnionArgInfo, _programName : string, description : string option,
79+
internal (argInfo : UnionArgInfo, _programName : string, helpTextMessage : string option,
7880
_usageStringCharacterWidth : int, errorHandler : IExiter) =
7981

80-
inherit ArgumentParser(argInfo, _programName, description, _usageStringCharacterWidth, errorHandler)
82+
inherit ArgumentParser(argInfo, _programName, helpTextMessage, _usageStringCharacterWidth, errorHandler)
8183

8284
// memoize parser generation for given template type
8385
static let argInfoLazy = lazy(preComputeUnionArgInfo<'Template> ())
8486

85-
let mkUsageString argInfo msgOpt = printUsage argInfo _programName _usageStringCharacterWidth msgOpt |> StringExpr.build
87+
let mkUsageString argInfo msgOpt = mkUsageString argInfo _programName _usageStringCharacterWidth msgOpt |> StringExpr.build
8688

8789
let (|ParserExn|_|) (e : exn) =
8890
match e with
8991
// do not display usage for App.Config parameter errors
9092
| ParseError (msg, ErrorCode.AppSettings, _) -> Some(ErrorCode.AppSettings, msg)
9193
| ParseError (msg, id, aI) -> Some (id, mkUsageString aI (Some msg))
92-
| HelpText aI -> Some (ErrorCode.HelpText, mkUsageString aI description)
94+
| HelpText aI -> Some (ErrorCode.HelpText, mkUsageString aI helpTextMessage)
9395
| _ -> None
9496

9597
/// <summary>
9698
/// Creates a new parser instance based on supplied F# union template.
9799
/// </summary>
98100
/// <param name="programName">Program identifier, e.g. 'cat'. Defaults to the current executable name.</param>
99-
/// <param name="description">Program description placed at the top of the usage string.</param>
101+
/// <param name="helpTextMessage">Message that will be displayed at the top of the help text.</param>
100102
/// <param name="usageStringCharacterWidth">Text width used when formatting the usage string. Defaults to 80 chars.</param>
101103
/// <param name="errorHandler">The implementation of IExiter used for error handling. Exception is default.</param>
102-
new (?programName : string, ?description : string, ?usageStringCharacterWidth : int, ?errorHandler : IExiter) =
104+
new (?programName : string, ?helpTextMessage : string, ?usageStringCharacterWidth : int, ?errorHandler : IExiter) =
103105
let usageStringCharacterWidth = defaultArg usageStringCharacterWidth 80
104106
let programName = match programName with Some pn -> pn | None -> currentProgramName.Value
105107
let errorHandler = match errorHandler with Some e -> e | None -> new ExceptionExiter() :> _
106-
new ArgumentParser<'Template>(argInfoLazy.Value, programName, description, usageStringCharacterWidth, errorHandler)
108+
new ArgumentParser<'Template>(argInfoLazy.Value, programName, helpTextMessage, usageStringCharacterWidth, errorHandler)
107109

108110
/// <summary>Parse command line arguments only.</summary>
109111
/// <param name="inputs">The command line input. Taken from System.Environment if not specified.</param>
@@ -117,11 +119,11 @@ and [<Sealed; NoEquality; NoComparison; AutoSerializable(false)>]
117119
let inputs = match inputs with None -> getEnvironmentCommandLineArgs () | Some args -> args
118120

119121
try
120-
let cliResults = parseCommandLine argInfo _programName description _usageStringCharacterWidth errorHandler raiseOnUsage ignoreUnrecognized inputs
122+
let cliResults = parseCommandLine argInfo _programName helpTextMessage _usageStringCharacterWidth errorHandler raiseOnUsage ignoreUnrecognized inputs
121123
let ignoreMissing = (cliResults.IsUsageRequested && not raiseOnUsage) || ignoreMissing
122124
let results = postProcessResults argInfo ignoreMissing None (Some cliResults)
123125

124-
new ParseResult<'Template>(argInfo, results, _programName, description, _usageStringCharacterWidth, errorHandler)
126+
new ParseResult<'Template>(argInfo, results, _programName, helpTextMessage, _usageStringCharacterWidth, errorHandler)
125127

126128
with ParserExn (errorCode, msg) -> errorHandler.Exit (msg, errorCode)
127129

@@ -135,7 +137,7 @@ and [<Sealed; NoEquality; NoComparison; AutoSerializable(false)>]
135137
let appSettingsResults = parseKeyValueConfig configurationReader argInfo
136138
let results = postProcessResults argInfo ignoreMissing (Some appSettingsResults) None
137139

138-
new ParseResult<'Template>(argInfo, results, _programName, description, _usageStringCharacterWidth, errorHandler)
140+
new ParseResult<'Template>(argInfo, results, _programName, helpTextMessage, _usageStringCharacterWidth, errorHandler)
139141

140142
with ParserExn (errorCode, msg) -> errorHandler.Exit (msg, errorCode)
141143

@@ -155,10 +157,10 @@ and [<Sealed; NoEquality; NoComparison; AutoSerializable(false)>]
155157

156158
try
157159
let appSettingsResults = parseKeyValueConfig configurationReader argInfo
158-
let cliResults = parseCommandLine argInfo _programName description _usageStringCharacterWidth errorHandler raiseOnUsage ignoreUnrecognized inputs
160+
let cliResults = parseCommandLine argInfo _programName helpTextMessage _usageStringCharacterWidth errorHandler raiseOnUsage ignoreUnrecognized inputs
159161
let results = postProcessResults argInfo ignoreMissing (Some appSettingsResults) (Some cliResults)
160162

161-
new ParseResult<'Template>(argInfo, results, _programName, description, _usageStringCharacterWidth, errorHandler)
163+
new ParseResult<'Template>(argInfo, results, _programName, helpTextMessage, _usageStringCharacterWidth, errorHandler)
162164

163165
with ParserExn (errorCode, msg) -> errorHandler.Exit (msg, errorCode)
164166

@@ -187,7 +189,7 @@ and [<Sealed; NoEquality; NoComparison; AutoSerializable(false)>]
187189
/// </summary>
188190
/// <param name="inputs">Argument input sequence.</param>
189191
member __.ToParseResult (inputs : seq<'Template>) : ParseResult<'Template> =
190-
mkParseResultFromValues argInfo errorHandler _usageStringCharacterWidth _programName description inputs
192+
mkParseResultFromValues argInfo errorHandler _usageStringCharacterWidth _programName helpTextMessage inputs
191193

192194
/// <summary>
193195
/// Gets a subparser associated with specific subcommand instance
@@ -198,7 +200,7 @@ and [<Sealed; NoEquality; NoComparison; AutoSerializable(false)>]
198200
let case = argInfo.Cases.[uci.Tag]
199201
match case.ParameterInfo with
200202
| NestedUnion (_,nestedUnion) ->
201-
new ArgumentParser<'SubTemplate>(nestedUnion, _programName, description, _usageStringCharacterWidth, errorHandler)
203+
new ArgumentParser<'SubTemplate>(nestedUnion, _programName, helpTextMessage, _usageStringCharacterWidth, errorHandler)
202204
| _ -> arguExn "internal error when fetching subparser %O." uci
203205

204206
/// <summary>
@@ -226,7 +228,7 @@ and [<Sealed; NoEquality; NoComparison; AutoSerializable(false)>]
226228

227229
/// <summary>Prints parameters in command line format. Useful for argument string generation.</summary>
228230
member __.PrintCommandLineArguments (args : 'Template list) : string [] =
229-
printCommandLineArgs argInfo (Seq.cast args) |> Seq.toArray
231+
mkCommandLineArgs argInfo (Seq.cast args) |> Seq.toArray
230232

231233
/// <summary>Prints parameters in command line format. Useful for argument string generation.</summary>
232234
member __.PrintCommandLineArgumentsFlat (args : 'Template list) : string =
@@ -237,7 +239,7 @@ and [<Sealed; NoEquality; NoComparison; AutoSerializable(false)>]
237239
/// <param name="printComments">Print XML comments over every configuration entry.</param>
238240
member __.PrintAppSettingsArguments (args : 'Template list, ?printComments : bool) : string =
239241
let printComments = defaultArg printComments true
240-
let xmlDoc = printAppSettings argInfo printComments args
242+
let xmlDoc = mkAppSettingsDocument argInfo printComments args
241243
use writer = { new System.IO.StringWriter() with member __.Encoding = System.Text.Encoding.UTF8 }
242244
xmlDoc.Save writer
243245
writer.Flush()
@@ -264,11 +266,11 @@ type ArgumentParser with
264266
/// which must be an F# Discriminated Union.
265267
/// </summary>
266268
/// <param name="programName">Program identifier, e.g. 'cat'. Defaults to the current executable name.</param>
267-
/// <param name="description">Program description placed at the top of the usage string.</param>
269+
/// <param name="helpTextMessage">Message that will be displayed at the top of the help text.</param>
268270
/// <param name="usageStringCharacterWidth">Text width used when formatting the usage string. Defaults to 80 chars.</param>
269271
/// <param name="errorHandler">The implementation of IExiter used for error handling. Exception is default.</param>
270-
static member Create<'Template when 'Template :> IArgParserTemplate>(?programName : string, ?description : string, ?usageStringCharacterWidth : int, ?errorHandler : IExiter) =
271-
new ArgumentParser<'Template>(?programName = programName, ?description = description, ?errorHandler = errorHandler, ?usageStringCharacterWidth = usageStringCharacterWidth)
272+
static member Create<'Template when 'Template :> IArgParserTemplate>(?programName : string, ?helpTextMessage : string, ?usageStringCharacterWidth : int, ?errorHandler : IExiter) =
273+
new ArgumentParser<'Template>(?programName = programName, ?helpTextMessage = helpTextMessage, ?errorHandler = errorHandler, ?usageStringCharacterWidth = usageStringCharacterWidth)
272274

273275

274276
[<AutoOpen>]

src/Argu/ParseResult.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ open FSharp.Quotations
77
type ParseResult<'Template when 'Template :> IArgParserTemplate>
88
internal (argInfo : UnionArgInfo, results : UnionParseResults, programName : string, description : string option, usageStringCharWidth : int, exiter : IExiter) =
99

10-
let mkUsageString message = printUsage argInfo programName usageStringCharWidth message |> StringExpr.build
10+
let mkUsageString message = mkUsageString argInfo programName usageStringCharWidth message |> StringExpr.build
1111

1212
// error handler functions
1313
let error hideUsage code msg =

src/Argu/Parsers.fs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,27 +117,27 @@ type CliParseState =
117117
}
118118

119119
/// parse the next command line argument and append to state
120-
let rec private parseCommandLinePartial (state : CliParseState) (argInfo : UnionArgInfo) (results : CliParseResultAggregator) =
120+
let rec private parseCommandLinePartial (state : CliParseState) (argInfo : UnionArgInfo) (aggregator : CliParseResultAggregator) =
121121
state.Reader.BeginCliSegment()
122122

123123
match state.Reader.GetNextToken false argInfo with
124124
| EndOfStream -> ()
125125
| HelpArgument _ when state.RaiseOnUsage -> raise <| HelpText argInfo
126-
| HelpArgument _ -> results.IsUsageRequested <- true
127-
| UnrecognizedOrArgument token when state.IgnoreUnrecognizedArgs -> results.AppendUnrecognized token
126+
| HelpArgument _ -> aggregator.IsUsageRequested <- true
127+
| UnrecognizedOrArgument token when state.IgnoreUnrecognizedArgs -> aggregator.AppendUnrecognized token
128128
| UnrecognizedOrArgument token -> error argInfo ErrorCode.CommandLine "unrecognized argument: '%s'." token
129129
| GroupedParams(_, switches) ->
130130
for sw in switches do
131131
let caseInfo = argInfo.CliParamIndex.Value.[sw]
132132
match caseInfo.ParameterInfo with
133133
| Primitives [||] ->
134-
let result = mkUnionCase caseInfo results.ResultCount ParseSource.CommandLine sw [||]
135-
results.AppendResult result
134+
let result = mkUnionCase caseInfo aggregator.ResultCount ParseSource.CommandLine sw [||]
135+
aggregator.AppendResult result
136136
| _ -> error argInfo ErrorCode.CommandLine "argument '%s' cannot be grouped with other switches." sw
137137

138138
| CliParam(_, name, caseInfo, Some _) when not caseInfo.IsEquals1Assignment ->
139139
error argInfo ErrorCode.CommandLine "invalid CLI syntax '%s=<param>'." name
140-
| CliParam(_, name, caseInfo, _) when caseInfo.IsFirst && results.ResultCount > 0 ->
140+
| CliParam(_, name, caseInfo, _) when caseInfo.IsFirst && aggregator.ResultCount > 0 ->
141141
error argInfo ErrorCode.CommandLine "argument '%s' should precede all other arguments." name
142142

143143
| CliParam(token, name, caseInfo, equalityParam) ->
@@ -150,8 +150,8 @@ let rec private parseCommandLinePartial (state : CliParseState) (argInfo : Union
150150
try field.Parser eqp
151151
with _ -> error argInfo ErrorCode.CommandLine "argument '%s' is assigned invalid value, should be <%s>." token field.Description
152152

153-
let result = mkUnionCase caseInfo results.ResultCount ParseSource.CommandLine name [| argument |]
154-
results.AppendResult result
153+
let result = mkUnionCase caseInfo aggregator.ResultCount ParseSource.CommandLine name [| argument |]
154+
aggregator.AppendResult result
155155

156156
| Primitives [|kf;vf|] when caseInfo.IsEquals2Assignment ->
157157
match state.Reader.GetNextToken true argInfo with
@@ -167,8 +167,8 @@ let rec private parseCommandLinePartial (state : CliParseState) (argInfo : Union
167167
try vf.Parser valTok
168168
with _ -> error argInfo ErrorCode.CommandLine "argument '%s' was given invalid value assignment '%s', should be <%s>." state.Reader.CurrentSegment token vf.Description
169169

170-
let result = mkUnionCase caseInfo results.ResultCount ParseSource.CommandLine name [|k;v|]
171-
results.AppendResult result
170+
let result = mkUnionCase caseInfo aggregator.ResultCount ParseSource.CommandLine name [|k;v|]
171+
aggregator.AppendResult result
172172
state.Reader.MoveNext()
173173
else
174174
error argInfo ErrorCode.CommandLine "argument '%s' must be followed by assignment '%s=%s'" caseInfo.Name kf.Description vf.Description
@@ -194,8 +194,8 @@ let rec private parseCommandLinePartial (state : CliParseState) (argInfo : Union
194194

195195
let parseSingleParameter () =
196196
let fields = fields |> Array.map parseNextField
197-
let result = mkUnionCase caseInfo results.ResultCount ParseSource.CommandLine name fields
198-
results.AppendResult result
197+
let result = mkUnionCase caseInfo aggregator.ResultCount ParseSource.CommandLine name fields
198+
aggregator.AppendResult result
199199

200200
if caseInfo.IsRest then
201201
while not state.Reader.IsCompleted do
@@ -215,8 +215,8 @@ let rec private parseCommandLinePartial (state : CliParseState) (argInfo : Union
215215

216216
argument :?> 'T |> Some :> obj }
217217

218-
let result = mkUnionCase caseInfo results.ResultCount ParseSource.CommandLine name [| optArgument |]
219-
results.AppendResult result
218+
let result = mkUnionCase caseInfo aggregator.ResultCount ParseSource.CommandLine name [| optArgument |]
219+
aggregator.AppendResult result
220220

221221
| OptionalParam(existential, field) ->
222222
let optArgument = existential.Accept { new IFunc<obj> with
@@ -229,8 +229,8 @@ let rec private parseCommandLinePartial (state : CliParseState) (argInfo : Union
229229

230230
| _ -> Option<'T>.None :> obj }
231231

232-
let result = mkUnionCase caseInfo results.ResultCount ParseSource.CommandLine name [| optArgument |]
233-
results.AppendResult result
232+
let result = mkUnionCase caseInfo aggregator.ResultCount ParseSource.CommandLine name [| optArgument |]
233+
aggregator.AppendResult result
234234

235235
| ListParam(existential, field) ->
236236
let listArg = existential.Accept { new IFunc<obj> with
@@ -252,8 +252,8 @@ let rec private parseCommandLinePartial (state : CliParseState) (argInfo : Union
252252
Seq.toList args :> obj
253253
}
254254

255-
let result = mkUnionCase caseInfo results.ResultCount ParseSource.CommandLine name [| listArg |]
256-
results.AppendResult result
255+
let result = mkUnionCase caseInfo aggregator.ResultCount ParseSource.CommandLine name [| listArg |]
256+
aggregator.AppendResult result
257257

258258
| NestedUnion (existential, nestedUnion) ->
259259
let nestedResults = parseCommandLineInner state nestedUnion
@@ -262,8 +262,8 @@ let rec private parseCommandLinePartial (state : CliParseState) (argInfo : Union
262262
member __.Invoke<'Template when 'Template :> IArgParserTemplate> () =
263263
new ParseResult<'Template>(nestedUnion, nestedResults, state.ProgramName, state.Description, state.UsageStringCharWidth, state.Exiter) :> obj }
264264

265-
let result = mkUnionCase caseInfo results.ResultCount ParseSource.CommandLine name [|result|]
266-
results.AppendResult result
265+
let result = mkUnionCase caseInfo aggregator.ResultCount ParseSource.CommandLine name [|result|]
266+
aggregator.AppendResult result
267267

268268
and private parseCommandLineInner (state : CliParseState) (argInfo : UnionArgInfo) =
269269
let results = new CliParseResultAggregator(argInfo)

0 commit comments

Comments
 (0)