11DOX_MARKER( [DOX_EXPR_TUT_CLI_INCLUDES])
13#include "alib/cli/commandline.hpp"
27DOX_MARKER( [DOX_EXPR_TUT_CLI_INCLUDES])
29DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS])
55 ErrUnknownCommand = 100,
56 ErrUnknownOption = 101,
57 ErrMissingFilename = 102,
58 ErrUnknownHelpTopic = 103,
59 ErrInternalError = 255,
62DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS])
64DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_ASSIGN])
70DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_ASSIGN])
72DOX_MARKER( [DOX_EXPR_TUT_CLI_CUSTOM_CAMP])
88 if( phase == BootstrapPhases::PrepareResources)
91 resourcePool->BootstrapBulk( ResourceCategory,
96 "Command line tool 'date'. V. {}.{} (in fact a sample application only)\n"
97 "(c) 2023-{} AWorx GmbH. Published under MIT License (Open Source).\n"
98 "For more information see: https://alib.dev\n"
104 "1," "now" ",1" "," ","
105 "2," "file" ",1" ",filename" ","
106 "99," "help" ",1" ",topic" ) EOS
108 "Commands<",
A_CHAR(
"datesample::Commands::"),
110 "THlpCmdSht_now",
A_CHAR(
"Reports the actual date/time"),
111 "THlpCmdLng_now",
A_CHAR(
"Reports the actual date/time. May be omitted, as this is the\n"
112 "default if no command is given.") EOS
114 "THlpCmdSht_file",
A_CHAR(
"Returns the date/time of a file. "),
115 "THlpCmdLng_file",
A_CHAR(
"Returns the last modification date/time of a file.") EOS
117 "THlpCmdSht_help",
A_CHAR(
"Displays usage information. "),
118 "THlpCmdLng_help",
A_CHAR(
"Displays usage information. Can also be given as an "
119 "option '--help'.") EOS
125 "0," "format" ",1," "f," "=" ",1," ","
126 "99," "help" ",1," "h," "=" ",0," ) EOS
128 "Options<",
A_CHAR(
"datesample::Options::"),
130 "TOptUsg_format",
A_CHAR(
"--format[=]\"placholders\""),
131 "TOptHlp_format",
A_CHAR(
"Sets the output format. The format specification is given with\n"
132 "documentation of ALib method CalendarDateTime::Format, found here:\n"
133 "https://alib.dev/classaworx_1_1lib_1_1system_1_1CalendarDateTime.html" ) ,
134 "TOptUsg_help" ,
A_CHAR(
"--help[[=]TOPIC]"),
135 "TOptHlp_help" ,
A_CHAR(
"Displays usage information.")
142 "0," "FILENAME" ",1," "" "," "=" "," ",-1" ",0" ","
143 "1," "TOPIC" ",1," "" "," "=" "," ",-1" ",1" ) EOS
145 "Parameters<",
A_CHAR(
"datesample::Parameters::"),
146 "THlpParSht_FILENAME",
A_CHAR(
"Mandatory parameter of command 'file."),
147 "THlpParLng_FILENAME",
A_CHAR(
"Denotes the file that is used for retrieving the modification date.\n"
148 "This parameter is mandatory to command file and has to be appended\n"
149 "to this command, separated by '='"),
150 "THlpParSht_TOPIC" ,
A_CHAR(
"Optional parameter of command (or option) 'help'."),
151 "THlpParLng_TOPIC" ,
A_CHAR(
"Denotes a specific toopic that the help command should be verbose about.")
158 "100," "ErrUnknownCommand" ",-1" ","
159 "101," "ErrUnknownOption" ",-1" ","
160 "102," "ErrMissingFilename" ",-1" ","
161 "103," "ErrUnknownHelpTopic" ",-1" ","
162 "255," "ErrInternalError" ",-1" ) EOS
164 "ExitCodes<",
A_CHAR(
"datesample::"),
166 "TExit0" ,
A_CHAR(
"Success (no error).")
167 ,
"TExit100" ,
A_CHAR(
"An unknown command was given. Valid commands are 'now' and 'file'")
168 ,
"TExit101" ,
A_CHAR(
"An unknown option was given. The only valid option is '--format='FORMATSPEC'.")
169 ,
"TExit102" ,
A_CHAR(
"Command 'file' given without a filename argument.")
170 ,
"TExit103" ,
A_CHAR(
"Command or option 'help' given without an unknown subtopic.")
171 ,
"TExit255" ,
A_CHAR(
"Unspecified internal error.")
175 "HlpCLIAppName",
A_CHAR(
"date"),
176 "HlpUsage" ,
A_CHAR(
"date [format=\"FORMATSPEC\" [now]|[file FILENAME]"),
177 "HlpHdlOpts" ,
A_CHAR(
"OPTIONS:" ),
178 "HlpHdlCmds" ,
A_CHAR(
"COMMANDS:" ),
179 "HlpHdlExtCds" ,
A_CHAR(
"EXIT CODES:" ),
180 "HlpHdlUsage" ,
A_CHAR(
"USAGE:" ),
181 "HlpHdlDscr" ,
A_CHAR(
"DESCRIPTION:" ),
182 "HlpHdlPDscr" ,
A_CHAR(
"PARAMETER DESCRIPTION:" ),
183 "HlpHdlTopic" ,
A_CHAR(
"Help on {} {!Q<>}:\n" ),
188 "This is a sample application provided with C++ library 'ALib'\n"
189 "to demonstrate the use of its sub-module \"ALib CLI\"."
197 else if( phase == BootstrapPhases::PrepareConfig )
211DOX_MARKER( [DOX_EXPR_TUT_CLI_CUSTOM_CAMP])
213DOX_MARKER( [DOX_EXPR_TUT_CLI_CUSTOM_CAMP_SINGLETON])
215extern SampleCamp SAMPLE_CAMP;
216SampleCamp SAMPLE_CAMP;
217DOX_MARKER( [DOX_EXPR_TUT_CLI_CUSTOM_CAMP_SINGLETON])
219DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_ASSIGN2])
225DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_ASSIGN2])
227DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_FWDDECL])
230DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_FWDDECL])
232DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_MAIN])
236int main(
int argc, const
char **argv )
249 Enum result= ExitCodes::ErrInternalError;
259 buffer.
AddMarked( SAMPLE_CAMP.GetResource(
"AppInfo" ),
266 cli.
Init( &SAMPLE_CAMP );
283 result= ExitCodes::ErrUnknownOption;
284 std::cerr <<
"Error: Unknown option given \""
286 <<
"\"" << std::endl;
291 result= processCLI( cli );
297 std::cerr << e.
Format() << std::endl;
301 catch(std::runtime_error& e)
303 result= ExitCodes::ErrInternalError;
304 std::cerr <<
"A runtime error occurred: " << e.what()<< std::endl;
312DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_MAIN])
315DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_PROCESS])
325 format <<
"yyyy-MM-dd HH:mm:ss";
338 if( !CLIUtil::GetHelp( cli,
nullptr, option, helpText ) )
340 std::cerr <<
"Error: Unknown help Topic \""
343 <<
"Usage Information follows: " << std::endl << std::endl;
344 option->
Args.Clear();
346 CLIUtil::GetHelp( cli,
nullptr, option, helpText );
348 std::cout << helpText.
Buffer << std::endl;
349 return ExitCodes::OK;
359 std::cerr <<
"Error: Unknown command given \""
361 <<
"\"" << std::endl;
362 return ExitCodes::ErrUnknownCommand;
368 calendar.
Format( format, printBuffer, lang::CurrentData::Clear );
369 std::cout << printBuffer << std::endl;
371 return ExitCodes::OK;
382 if ( actCmdCode == Commands::Now )
387 else if ( actCmdCode == Commands::File )
392 std::cerr <<
"Error: no filename given with command 'file'" << std::endl;
393 std::cerr <<
"Usage: " << CLIUtil::GetCommandUsageFormat(cli, *actCmd->
Declaration )
395 return ExitCodes::ErrMissingFilename;
400 std::filesystem::path path( name.Terminate() );
401 dt.
Import( std::chrono::clock_cast<std::chrono::system_clock>(
402 std::filesystem::last_write_time( path ) ) ) ;
405 else if ( actCmdCode == Commands::Help )
407 if( !CLIUtil::GetHelp( cli, actCmd,
nullptr, helpText ) )
409 std::cerr <<
"Error: Unknown help topic" << std::endl;
410 std::cerr <<
"Usage: " << CLIUtil::GetCommandUsageFormat(cli, *actCmd->
Declaration )
412 return ExitCodes::ErrUnknownHelpTopic;
414 std::cout << helpText.
Buffer << std::endl;
422 calendar.
Format( format, printBuffer, lang::CurrentData::Clear );
423 std::cout << printBuffer << std::endl;
425 return ExitCodes::OK;
427DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_PROCESS])
const Enum & Element() const
void Init(lang::Camp *resModule)
virtual ALIB_API Command * NextCommand()
List< MonoAllocator, Command * > CommandsParsed
A list of commands actually parsed. Filled with method ReadNextCommands.
MonoAllocator & GetAllocator() noexcept
StdVectorMono< integer > ArgsLeft
List< MonoAllocator, String, Recycling::Shared > OptionArgsIgnored
virtual ALIB_API void ReadOptions()
ALIB_API Option * GetOption(Enum element)
StdVectorMono< String > ArgStrings
virtual ALIB_API void ReadNextCommands()
ALIB_API Message & Back() const
ALIB_API AString & Format(AString &target) const
ALIB_API AString & Format(Substring format, AString &target, lang::CurrentData targetData=lang::CurrentData::Keep) const
void Allocate(TAllocator &allocator, const TString< TChar > ©)
void Import(TTimePoint timePoint)
#define ALIB_ENUMS_ASSIGN_RECORD(TEnum, TRecord)
#define ALIB_RESOURCED_IN_MODULE(T, Camp, ResName)
@ Filename
Denotes the actual source file as the scope.
ShutdownPhases
Termination levels usable with Bootstrapping ALib Camps.
BootstrapPhases
Initialization levels usable with Bootstrapping ALib Camps.
ALIB_API List< MonoAllocator, lang::Camp * > CAMPS
void BootstrapAddDefaultCamps()
ALIB_WARNINGS_RESTORE void Bootstrap(int alibVersion, int alibRevision, TCompilationFlags compilationFlags)
A command of a ALib CLI command line.
List< MonoAllocator, Parameter *, Recycling::Shared > ParametersMandatory
Mandatory parameters parsed.
CommandDecl * Declaration
The underlying declaration.
ALib Enum Record type used by class CommandDecl.
ALib Enum Record type used by class OptionDecl.
ALib Enum Record type used by class ParameterDecl.
List< MonoAllocator, String, Recycling::Shared > Args
Arguments belonging to this option.