alexghiondea / commandline Goto Github PK
View Code? Open in Web Editor NEWCommandLine parser
License: MIT License
CommandLine parser
License: MIT License
Right now, calling Parse
will return a null object if something bad happened during the parse.
An error message is displayed to the user that contains the exception.
However, there isn't a good way to catch the exception that was begin thrown from the parser.
Thoughts?
Example:
<action_group_arg> <required_1> <required_2> <required_3> <optional_w_bad_syntax>
If someone were to supply a malformed optional argument such as "-option=value" then it will report that the previous argument was not found. In this case that is a required argument, so the error message is misleading & incorrect. This is caused by line 297 in Parser.cs, because it indexes into args with currentLogicalPosition without adding offsetInArray.
Using the above example syntax we can expand on the problem here:
Foo bar_req1 bar_req2 bar_re3 -optional=value
Would result in the following exception msg: "Could not find argument bar_req3"
For code like this:
namespace DotNetVersions.Console
{
class Options
{
[ArgumentGroup(nameof(Action.search))]
[RequiredArgument(0, "version", "The file version you are interested in")]
public string Version { get; set; }
[ActionArgument]
public Action Action { get; set; }
}
enum Action
{
list,
search
}
}
There is a crash in the analyzer with this callstack:
Warning AD0001 Analyzer 'CommandLine.Analyzer.CommandLineAnalyzer' threw an exception of type 'System.Collections.Generic.KeyNotFoundException' with message 'The given key was not present in the dictionary.'.
'CommandLine.Analyzer.CommandLineAnalyzer' threw the following exception:
'Exception occurred with following context:
Compilation: fooBar
ISymbol: Options (NamedType)
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at CommandLine.Analyzer.CommandLineAnalyzer.CreateArgumentMapForType(INamedTypeSymbol namedTypeSymbol, SymbolAnalysisContext context, ActionArgument& actionArg)
at CommandLine.Analyzer.CommandLineAnalyzer.AnalyzeCommandLineType(SymbolAnalysisContext context)
at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c.<ExecuteSymbolActionsCore>b__38_1(ValueTuple`2 data)
at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
----
First off, great project, thanks. A suggestion though, if you don't mind me asking.
When i have my command line parsed, i will get a filepath; i then need to check this filepath exists and the config file at that location parses okay, if not then i want to be able to display an error and the Detailed Help already formatted as per an error generated from the parser.
Can you add this facility so that help can be called externally?
When using parameters, sometimes you would need to tweak them based on some rules.
One example is the use of the output file. The user will provide a value for that, but what happens if the file already exists?
An approach is to automatically append a number to it until it creates a file name that no longer exists.
Other validations could be added -- what if a parameter can have a min/max value? That can be validated by the parser.
Couple of approaches:
internal class CmdLineArgs
{
[ActionArgument]
public Action Action { get; set; }
[CommonArgument]
[OptionalArgument(null, "token", "The GitHub authentication token. If not specified, we will authorize to GitHub.")]
public string Token { get; set; }
[ArgumentGroup(nameof(Action.List))]
[RequiredArgument(0, "repos", "The list of repositories where to add the milestones to. The format is: owner\\repoName.", true)]
public List<string> Repos { get; set; }
[ArgumentGroup(nameof(Action.Create))]
[RequiredArgument(0, "milestoneInputFile", "The file containing the list of milestones to create.")]
public string MilestoneFile { get; set; }
[ArgumentGroup(nameof(Action.Create))]
[RequiredArgument(1, "repos", "The list of repositories where to add the milestones to. The format is: owner\\repoName.", true)]
public List<string> Repositories { get; set; }
}
public enum Action
{
Create,
List
}
In the above example, I would like the Repositories
property to be required at position 1 in the Create
group and position 0 in the List
This could be achieved by adding an argument on the ArgumentGroup
attribute:
[ArgumentGroup(nameof(Action.Create), OverrideRequiredPosition=0)]
For help, we should not return true
for TryParse
as we didn't actually correctly parse the arguments.
E:\repos\ApplyNullableDecorators\src>apply-nullable-decorators apply E:\repos\coreclr\src\System.Private.CoreLib\System.Private.CoreLib.csproj System.Int32 santi -msbuild ""
Error: Index was outside the bounds of the array.
Usage:
apply-nullable-decorators.exe apply project type [-msbuild value]
apply-nullable-decorators.exe apistats project jetbrainsfiles [-msbuild value]
For detailed information run 'apply-nullable-decorators --help'.
Hi, it appears this library doesn't handle a string array, i.e. where args[6] for example? If I join the string we still get no matches.
Example:
/// <summary>
/// Type of action by files
/// </summary>
[RequiredArgument(0, "action", "what need do: convert or unconvert")]
public ActionType ActionType { get; private set; }
/// <summary>
/// Action type
/// </summary>
public enum ActionType
{
Convert,
UnConvert
}
if i used program.exe convert
- it generate error, but if used program.exe Convert
work right
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.