You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
📝 Todo : ** create a proper automagic help command **
Description:
Code Snippet:
// ---------------------------------------------------------------------------------------------------------------------// Imports// ---------------------------------------------------------------------------------------------------------------------usingCodeOfChaos.CliArgsParser.Contracts;usingSystem.Collections.Frozen;usingSystem.Text.RegularExpressions;namespaceCodeOfChaos.CliArgsParser;// ---------------------------------------------------------------------------------------------------------------------// Code// ---------------------------------------------------------------------------------------------------------------------publicpartialclassCliArgsParser{privatereadonlyIUserInputRegistry_userInputRegistry=newUserInputRegistry();publicrequiredFrozenDictionary<string,(CommandData,INonGenericCommandInterfaces)>CommandLookup{get;init;}public(CommandDataCommandData,INonGenericCommandInterfacesCommandObject)?StartupCommand{get;init;}publicrequiredboolHasCustomExitCommand{get;init;}publicrequiredboolHasCustomHelpCommand{get;init;}[GeneratedRegex(@"\s+")]privatestaticpartialRegexFindEmptySpacesRegex{get;}// -----------------------------------------------------------------------------------------------------------------// Methods// -----------------------------------------------------------------------------------------------------------------publicTaskParseAsync(string[]args)=>ParseAsync(InputHelper.ToOneLine(args));publicasyncTaskParseAsync(stringargs){if(string.IsNullOrWhiteSpace(args)){thrownewArgumentException("Arguments cannot be null or empty.",nameof(args));}// Split the input into tokensstring[]tokens=FindEmptySpacesRegex.Split(args);if(tokens.Length==0||string.IsNullOrWhiteSpace(tokens[0])){thrownewArgumentException("Invalid arguments provided.",nameof(args));}// Extract the command name (assume the first token is the command name)stringcommandName=tokens[0];// Check if the command exists in the CommandLookupif(!CommandLookup.TryGetValue(commandName,out(CommandDataCommandData,INonGenericCommandInterfacesCommandObject)commandEntry)){thrownewKeyNotFoundException($"Command '{commandName}' not found.");}// For performance reasons, disposing the registry cleans the internal stuff up so we can reuse the same objectusingIUserInputRegistryregistry=_userInputRegistry;registry.IngestString(tokens.Skip(1));// Call the InitializeAsync methodawaitcommandEntry.CommandObject.InitializeAsync(registry);}publicTaskParseStartupArgs(string[]args)=>ParseStartupArgs(string.Join(" ",args));publicasyncTaskParseStartupArgs(stringargs){if(StartupCommandis not {CommandObject:varcommandObject})thrownewException("No startup command set.");usingIUserInputRegistryregistry=_userInputRegistry;registry.IngestString(args);awaitcommandObject.InitializeAsync(_userInputRegistry);}publicasyncTaskStartUserInputMode(){// Inform user about the input modeConsole.WriteLine("Entering interactive user input mode. Type your commands below. Type 'exit' to quit.");while(true){usingIUserInputRegistryregistry=_userInputRegistry;// Prompt the user for inputConsole.Write("$:> ");stringinput=Console.ReadLine()?.Trim()??string.Empty;// Skip empty inputif(string.IsNullOrWhiteSpace(input)){Console.WriteLine("No command entered, please try again.");continue;}try{if(!HasCustomExitCommand&&input.Equals("exit")){// TODO create a proper automagic exit commandConsole.WriteLine("Exiting interactive user input mode.");break;}if(!HasCustomHelpCommand&&input.Equals("help")){// TODO create a proper automagic help commandConsole.WriteLine("Available commands:");foreach(stringcommandNameinCommandLookup.Keys){Console.WriteLine($" - {commandName}");}continue;}// Parse and execute the commandawaitParseAsync(input);}catch(Exceptionex){// Handle possible errors during command executionConsole.WriteLine($"Error: {ex.Message}");}}}}
📝 Todo : ** create a proper automagic help command **
Description:
Code Snippet:
File:
cs-code_of_chaos-cli_args_parser/src/CodeOfChaos.CliArgsParser/CliArgsParser.cs
Line 88 in f9c9e34
The text was updated successfully, but these errors were encountered: