54 ErrUnknownCommand = 100,
55 ErrUnknownOption = 101,
56 ErrMissingFilename = 102,
57 ErrUnknownHelpTopic = 103,
58 ErrInternalError = 255,
87 if( phase == BootstrapPhases::PrepareResources)
90 resourcePool->BootstrapBulk( ResourceCategory,
95 "Command line tool 'date'. V. {}.{} (in fact a sample application only)\n"
96 "(c) 2023-{} AWorx GmbH. Published under MIT License (Open Source).\n"
97 "For more information see: https://alib.dev\n"
103 "1," "now" ",1" "," ","
104 "2," "file" ",1" ",filename" ","
105 "99," "help" ",1" ",topic" ) EOS
107 "Commands<",
A_CHAR(
"datesample::Commands::"),
109 "THlpCmdSht_now",
A_CHAR(
"Reports the actual date/time"),
110 "THlpCmdLng_now",
A_CHAR(
"Reports the actual date/time. May be omitted, as this is the\n"
111 "default if no command is given.") EOS
113 "THlpCmdSht_file",
A_CHAR(
"Returns the date/time of a file. "),
114 "THlpCmdLng_file",
A_CHAR(
"Returns the last modification date/time of a file.") EOS
116 "THlpCmdSht_help",
A_CHAR(
"Displays usage information. "),
117 "THlpCmdLng_help",
A_CHAR(
"Displays usage information. Can also be given as an "
118 "option '--help'.") EOS
124 "0," "format" ",1," "f," "=" ",1," ","
125 "99," "help" ",1," "h," "=" ",0," ) EOS
127 "Options<",
A_CHAR(
"datesample::Options::"),
129 "TOptUsg_format",
A_CHAR(
"--format[=]\"placholders\""),
130 "TOptHlp_format",
A_CHAR(
"Sets the output format. The format specification is given with\n"
131 "documentation of ALib method CalendarDateTime::Format, found here:\n"
132 "https://alib.dev/classaworx_1_1lib_1_1system_1_1CalendarDateTime.html" ) ,
133 "TOptUsg_help" ,
A_CHAR(
"--help[[=]TOPIC]"),
134 "TOptHlp_help" ,
A_CHAR(
"Displays usage information.")
141 "0," "FILENAME" ",1," "" "," "=" "," ",-1" ",0" ","
142 "1," "TOPIC" ",1," "" "," "=" "," ",-1" ",1" ) EOS
144 "Parameters<",
A_CHAR(
"datesample::Parameters::"),
145 "THlpParSht_FILENAME",
A_CHAR(
"Mandatory parameter of command 'file."),
146 "THlpParLng_FILENAME",
A_CHAR(
"Denotes the file that is used for retrieving the modification date.\n"
147 "This parameter is mandatory to command file and has to be appended\n"
148 "to this command, separated by '='"),
149 "THlpParSht_TOPIC" ,
A_CHAR(
"Optional parameter of command (or option) 'help'."),
150 "THlpParLng_TOPIC" ,
A_CHAR(
"Denotes a specific toopic that the help command should be verbose about.")
157 "100," "ErrUnknownCommand" ",-1" ","
158 "101," "ErrUnknownOption" ",-1" ","
159 "102," "ErrMissingFilename" ",-1" ","
160 "103," "ErrUnknownHelpTopic" ",-1" ","
161 "255," "ErrInternalError" ",-1" ) EOS
163 "ExitCodes<",
A_CHAR(
"datesample::"),
165 "TExit0" ,
A_CHAR(
"Success (no error).")
166 ,
"TExit100" ,
A_CHAR(
"An unknown command was given. Valid commands are 'now' and 'file'")
167 ,
"TExit101" ,
A_CHAR(
"An unknown option was given. The only valid option is '--format='FORMATSPEC'.")
168 ,
"TExit102" ,
A_CHAR(
"Command 'file' given without a filename argument.")
169 ,
"TExit103" ,
A_CHAR(
"Command or option 'help' given without an unknown sub-topic.")
170 ,
"TExit255" ,
A_CHAR(
"Unspecified internal error.")
174 "HlpCLIAppName",
A_CHAR(
"date"),
175 "HlpUsage" ,
A_CHAR(
"date [format=\"FORMATSPEC\" [now]|[file FILENAME]"),
176 "HlpHdlOpts" ,
A_CHAR(
"OPTIONS:" ),
177 "HlpHdlCmds" ,
A_CHAR(
"COMMANDS:" ),
178 "HlpHdlExtCds" ,
A_CHAR(
"EXIT CODES:" ),
179 "HlpHdlUsage" ,
A_CHAR(
"USAGE:" ),
180 "HlpHdlDscr" ,
A_CHAR(
"DESCRIPTION:" ),
181 "HlpHdlPDscr" ,
A_CHAR(
"PARAMETER DESCRIPTION:" ),
182 "HlpHdlTopic" ,
A_CHAR(
"Help on {} {!Q<>}:\n" ),
187 "This is a sample application provided with C++ library 'ALib'\n"
188 "to demonstrate the use of its sub-module \"ALib CLI\"."
196 else if( phase == BootstrapPhases::PrepareConfig )
214extern SampleCamp SAMPLE_CAMP;
215SampleCamp SAMPLE_CAMP;
235int main(
int argc, const
char **argv )
248 Enum result= ExitCodes::ErrInternalError;
258 buffer.
AddMarked( SAMPLE_CAMP.GetResource(
"AppInfo" ),
265 cli.
Init( &SAMPLE_CAMP );
282 result= ExitCodes::ErrUnknownOption;
283 std::cerr <<
"Error: Unknown option given \""
285 <<
"\"" << std::endl;
290 result= processCLI( cli );
296 std::cerr << e.
Format() << std::endl;
300 catch(std::runtime_error& e)
302 result= ExitCodes::ErrInternalError;
303 std::cerr <<
"A runtime error occurred: " << e.what()<< std::endl;
324 format <<
"yyyy-MM-dd HH:mm:ss";
337 if( !CLIUtil::GetHelp( cli,
nullptr, option, helpText ) )
339 std::cerr <<
"Error: Unknown help Topic \""
342 <<
"Usage Information follows: " << std::endl << std::endl;
343 option->
Args.Clear();
345 CLIUtil::GetHelp( cli,
nullptr, option, helpText );
347 std::cout << helpText.
Buffer << std::endl;
348 return ExitCodes::OK;
358 std::cerr <<
"Error: Unknown command given \""
360 <<
"\"" << std::endl;
361 return ExitCodes::ErrUnknownCommand;
367 calendar.
Format( format, printBuffer, lang::CurrentData::Clear );
368 std::cout << printBuffer << std::endl;
370 return ExitCodes::OK;
381 if ( actCmdCode == Commands::Now )
386 else if ( actCmdCode == Commands::File )
391 std::cerr <<
"Error: no filename given with command 'file'" << std::endl;
392 std::cerr <<
"Usage: " << CLIUtil::GetCommandUsageFormat(cli, *actCmd->
Declaration )
394 return ExitCodes::ErrMissingFilename;
399 std::filesystem::path path( name.Terminate() );
400 dt.
Import( std::chrono::clock_cast<std::chrono::system_clock>(
401 std::filesystem::last_write_time( path ) ) ) ;
404 else if ( actCmdCode == Commands::Help )
406 if( !CLIUtil::GetHelp( cli, actCmd,
nullptr, helpText ) )
408 std::cerr <<
"Error: Unknown help topic" << std::endl;
409 std::cerr <<
"Usage: " << CLIUtil::GetCommandUsageFormat(cli, *actCmd->
Declaration )
411 return ExitCodes::ErrUnknownHelpTopic;
413 std::cout << helpText.
Buffer << std::endl;
421 calendar.
Format( format, printBuffer, lang::CurrentData::Clear );
422 std::cout << printBuffer << std::endl;
424 return ExitCodes::OK;
const Enum & Element() const
void Init(lang::Camp *resModule)
virtual ALIB_API Command * NextCommand()
List< Command * > CommandsParsed
virtual ALIB_API void ReadOptions()
std::vector< String, StdContMA< String > > ArgStrings
MonoAllocator & GetAllocator()
ALIB_API Option * GetOption(Enum element)
virtual ALIB_API void ReadNextCommands()
List< String, Recycling::Shared > OptionArgsIgnored
std::vector< integer, StdContMA< integer > > ArgsLeft
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
strings::TString< TChar > EmplaceString(const strings::TString< TChar > &src)
void Import(TTimePoint timePoint)
#define ALIB_ENUMS_ASSIGN_RECORD(TEnum, TRecord)
#define ALIB_RESOURCED_IN_MODULE(T, Camp, ResName)
ALIB_API List< lang::Camp * > Camps
void BootstrapAddDefaultCamps()
ALIB_WARNINGS_RESTORE void Bootstrap(int alibVersion, int alibRevision, TCompilationFlags compilationFlags)
List< Parameter *, Recycling::Shared > ParametersMandatory
CommandDecl * Declaration
List< String, Recycling::Shared > Args