public void WriteUsage(GlobalOptionsWrapper globalOptions, bool options, string command) { Console.Write($"Usage: {Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location)} "); if (globalOptions.GlobalOptions != null) { Console.Write("[OPTIONS] "); } Console.WriteLine("COMMAND"); Console.WriteLine(); }
public void WriteGlobalOptions(GlobalOptionsWrapper globalOptions) { Console.WriteLine("Options:"); var written = new HashSet <GlobalOptionAttribute>(); foreach (var option in globalOptions.Options.Select(x => x.Value)) { if (written.Contains(option)) { continue; } var type = GetTypeFromCLR(option.Property.PropertyType); WriteOption(option.ShortForm, option.LongForm, type, option.HelpText); written.Add(option); } }
void ICommandHelper.WriteVerbHelp(VerbAttribute verb, ICommandExecutorOptions options, GlobalOptionsWrapper globalOptions) { var sortedDictionary = new SortedDictionary <string, string>(); _commandHelper.GetVerbHelp(verb, "", sortedDictionary); if (verb.IsRoot) { WriteUsage(globalOptions, globalOptions.GlobalOptions != null, "COMMAND"); WriteGlobalOptions(globalOptions); } Console.WriteLine(); var commandGroups = verb.Commands.Select(x => x.Value).Distinct().Select(x => new { x.Category, Name = x.GetName(), x.HelpText }).Concat(verb.Verbs.Select(x => x.Value).Distinct().Select(x => new { x.Category, Name = x.GetName(), x.HelpText })).OrderBy(x => x.Name).GroupBy(x => x.Category); foreach (var commandGroup in commandGroups) { Console.WriteLine((commandGroup.Key ?? "Commands") + ":"); foreach (var command in commandGroup) { Console.CursorLeft = 2; Console.Write(command.Name); foreach (var line in GetLines(command.HelpText, 55)) { Console.CursorLeft = 14; Console.Write(line); } Console.WriteLine(); } Console.WriteLine(); } Console.WriteLine($"For help with command syntax, type `COMMAND --{options.HelpLongForm}` or `COMMAND -{options.HelpShortForm}`"); }
bool ICommandHelper.TryShowHelpOrVersion(CommandPart commandPart, VerbAttribute verb, string key, ICommandExecutorOptions options, GlobalOptionsWrapper globalOptions) { if ((commandPart.IsShortForm && key[0] == options.VersionShortForm) || (!commandPart.IsShortForm && key.Equals(options.VersionLongForm, StringComparison.OrdinalIgnoreCase))) { _commandHelper.WriteVersion(options); return(true); } if ((commandPart.IsShortForm && key[0] == options.HelpShortForm) || (!commandPart.IsShortForm && key.Equals(options.HelpLongForm, StringComparison.OrdinalIgnoreCase))) { _commandHelper.WriteVerbHelp(verb, options, globalOptions); return(true); } return(false); }
void ICommandHelper.WriteCommandHelp(CommandAttribute command, ICommandExecutorOptions options, GlobalOptionsWrapper globalOptions) { Console.Write($"Usage: {command.GetName()}"); if (options.ValuesFirst) { foreach (var value in command.Values) { Console.Write($" {{{value.Parameter.Name}}}"); } } if (command.Options.Count > 0) { Console.Write(" [Options]"); } if (!options.ValuesFirst) { foreach (var value in command.Values) { Console.Write($" {{{value.Parameter.Name}}}"); } } Console.WriteLine(); Console.WriteLine(); Console.WriteLine(command.HelpText); if (command.Values.Count > 0) { Console.WriteLine(); Console.WriteLine("Values:"); foreach (var value in command.Values) { Console.WriteLine($" {value.GetName(),-20}{value.HelpText}"); } } if (command.Options.Count == 0) { return; } Console.WriteLine(); Console.WriteLine("Options:"); var written = new HashSet <OptionAttribute>(); foreach (var option in command.Options) { if (written.Contains(option.Value)) { continue; } Console.Write(" "); string name = " "; if (option.Value.ShortForm != '\0') { name = " -" + option.Value.ShortForm; } if (option.Value.LongForm != null) { name += " --" + option.Value.LongForm + (option.Value.Parameter.ParameterType == typeof(bool) ? "" : "=<value>"); } var helpText = option.Value.HelpText; if (option.Value.Parameter.ParameterType.IsArray) { helpText = "(Array) " + helpText; } Console.WriteLine(name.PadRight(30, ' ') + helpText); written.Add(option.Value); } Console.WriteLine(); }
async Task ICommandExecutorImpl.ExecuteCommandAsync(CommandAttribute command, IEnumerator <CommandPart> commandParts, GlobalOptionsWrapper globalOptions) { var parameters = new SortedList <int, object>(); ParameterInfo lastParameter = null; while (commandParts.MoveNext()) { var key = _parser.GetString(commandParts.Current.Key); if (commandParts.Current.IsArgument && _commandHelper.TryShowHelpOrVersion(commandParts.Current, command, key, _options, globalOptions)) { return; } if (commandParts.Current.IsArgument) { if (_impl.TrySetGlobalOption(key, commandParts.Current, globalOptions)) { continue; } lastParameter = _impl.SetOptionParameter(key, commandParts.Current, command, parameters); } else { lastParameter = _impl.SetValueParameter(key, command, parameters, lastParameter); } } _impl.AddDefaultValues(command, parameters); if (globalOptions.GlobalOptions != null && command.GlobalOptionsParameter != null) { parameters[command.GlobalOptionsParameter.Position] = globalOptions.GlobalOptions; } await _impl.ExecuteActualCommandAsync(command, parameters); return; }
bool ICommandExecutorImpl.TrySetGlobalOption(string key, CommandPart commandPart, GlobalOptionsWrapper globalOptions) { if (globalOptions?.Options == null || !globalOptions.Options.TryGetValue(key, out var optionAtt)) { return(false); } var value = _parser.GetString(commandPart.Value); var globalvalue = _valueConverter.GetValue(value, optionAtt.Property.PropertyType); optionAtt.Property.SetValue(globalOptions.GlobalOptions, globalvalue); return(true); }
async Task ICommandExecutorImpl.ExecuteInternalAsync(VerbAttribute verb, IEnumerator <CommandPart> commandParts, GlobalOptionsWrapper globalOptions) { if (!commandParts.MoveNext()) { throw new BadCommandException(verb, "Malformed command"); } if (commandParts.Current.IsArgument) { var key = _parser.GetString(commandParts.Current.Key); if (_impl.TrySetGlobalOption(key, commandParts.Current, globalOptions)) { await _impl.ExecuteInternalAsync(verb, commandParts, globalOptions); return; } if (_commandHelper.TryShowHelpOrVersion(commandParts.Current, verb, key, _options, globalOptions)) { return; } throw new BadCommandException(verb, "Unexpected option"); } var name = _parser.GetString(commandParts.Current.Key); if (verb.Verbs.TryGetValue(name, out var nextVerb)) { await _impl.ExecuteInternalAsync(nextVerb, commandParts, globalOptions); return; } if (verb.Commands.TryGetValue(name, out var nextCommand)) { await _impl.ExecuteCommandAsync(nextCommand, commandParts, globalOptions); return; } throw new BadCommandException(verb, name); }