Skip to content

Commit

Permalink
#89 add SkipFirstArg() method to configure the parser to skip the fir…
Browse files Browse the repository at this point in the history
…st argument.

Sometimes this is required when Windows as part of args passed into an application includes the executable name in the first element.
This executable name is in most circumstances not required and consumers of FCLP must skip it manually before passing the arguments into the parser.
  • Loading branch information
siywilliams committed Nov 26, 2017
1 parent 2eedaf6 commit 981698b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
20 changes: 20 additions & 0 deletions FluentCommandLineParser/FluentCommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,29 @@ public IFluentCommandLineParser UseOwnOptionPrefix(params string[] prefix)
return this;
}

/// <summary>
/// Configures the <see cref="IFluentCommandLineParser"/> to skip the first of the specified arguments.
/// This can be useful when Windows inserts the application name in the command line arguments for your application.
/// </summary>
/// <returns>this <see cref="IFluentCommandLineParser"/></returns>
public IFluentCommandLineParser SkipFirstArg()
{
SkipTheFirstArg = true;
return this;
}

/// <summary>
/// Gets the <see cref="StringComparison"/> to use when matching values.
/// </summary>
internal StringComparison StringComparison { get; private set; }

/// <summary>
/// Gets or sets whether to skip the first of the specified arguments.
/// This can be useful when Windows inserts the application name in the command line arguments for your application.
/// </summary>
/// <returns><c>true</c> if the first arg is to be ignored; otherwise <c>false</c>.</returns>
internal bool SkipTheFirstArg { get; set; }

/// <summary>
/// Gets the list of Options
/// </summary>
Expand Down Expand Up @@ -308,6 +326,8 @@ public ICommandLineOptionFluent<T> Setup<T>(string longOption)
/// <returns>An <see cref="ICommandLineParserResult"/> representing the results of the parse operation.</returns>
public ICommandLineParserResult Parse(string[] args)
{
if (SkipTheFirstArg) args = args.Skip(1).ToArray();

var parserEngineResult = this.ParserEngine.Parse(args, HasCommands);
var parsedOptions = parserEngineResult.ParsedOptions.ToList();

Expand Down
11 changes: 11 additions & 0 deletions FluentCommandLineParser/FluentCommandLineParserT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,16 @@ public IFluentCommandLineParser<TBuildType> UseOwnOptionPrefix(params string[] p
Parser.UseOwnOptionPrefix(prefix);
return this;
}

/// <summary>
/// Configures the <see cref="IFluentCommandLineParser"/> to skip the first of the specified arguments.
/// This can be useful when Windows inserts the application name in the command line arguments for your application.
/// </summary>
/// <returns>this <see cref="IFluentCommandLineParser{TBuildType}"/></returns>
public IFluentCommandLineParser<TBuildType> SkipFirstArg()
{
Parser.SkipFirstArg();
return this;
}
}
}
9 changes: 8 additions & 1 deletion FluentCommandLineParser/IFluentCommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,12 @@ public interface IFluentCommandLineParser : ICommandLineOptionSetupFactory, ICom
/// <param name="prefix"></param>
/// <returns></returns>
IFluentCommandLineParser UseOwnOptionPrefix(params string[] prefix);
}

/// <summary>
/// Configures the <see cref="IFluentCommandLineParser"/> to skip the first of the specified arguments.
/// This can be useful when Windows inserts the application name in the command line arguments for your application.
/// </summary>
/// <returns>this <see cref="IFluentCommandLineParser"/></returns>
IFluentCommandLineParser SkipFirstArg();
}
}
31 changes: 19 additions & 12 deletions FluentCommandLineParser/IFluentCommandLineParserT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,31 @@ namespace Fclp
/// </summary>
IEnumerable<ICommandLineOption> Options { get; }

/// <summary>
/// Configures the <see cref="IFluentCommandLineParser"/> so that short and long options that differ by case are considered the same.
/// </summary>
/// <returns></returns>
IFluentCommandLineParser<TBuildType> MakeCaseInsensitive();
/// <summary>
/// Configures the <see cref="IFluentCommandLineParser{TBuildType}"/> so that short and long options that differ by case are considered the same.
/// </summary>
/// <returns></returns>
IFluentCommandLineParser<TBuildType> MakeCaseInsensitive();

/// <summary>
/// Configures the <see cref="IFluentCommandLineParser"/> so that short options are treated the same as long options, thus
/// unique short option behaviour is ignored.
/// </summary>
/// <returns></returns>
IFluentCommandLineParser<TBuildType> DisableShortOptions();
/// <summary>
/// Configures the <see cref="IFluentCommandLineParser{TBuildType}"/> so that short options are treated the same as long options, thus
/// unique short option behaviour is ignored.
/// </summary>
/// <returns></returns>
IFluentCommandLineParser<TBuildType> DisableShortOptions();

/// <summary>
/// Configures the <see cref="IFluentCommandLineParser"/> to use the specified option prefixes instead of the default.
/// Configures the <see cref="IFluentCommandLineParser{TBuildType}"/> to use the specified option prefixes instead of the default.
/// </summary>
/// <param name="prefix"></param>
/// <returns></returns>
IFluentCommandLineParser<TBuildType> UseOwnOptionPrefix(params string[] prefix);

/// <summary>
/// Configures the <see cref="IFluentCommandLineParser"/> to skip the first of the specified arguments.
/// This can be useful when Windows inserts the application name in the command line arguments for your application.
/// </summary>
/// <returns>this <see cref="IFluentCommandLineParser{TBuildType}"/></returns>
IFluentCommandLineParser<TBuildType> SkipFirstArg();
}
}

0 comments on commit 981698b

Please sign in to comment.