11DOX_MARKER( [DOX_EXPR_TUT_CLI_INCLUDES])
13#include "ALib.Strings.StdIOStream.H"
29DOX_MARKER( [DOX_EXPR_TUT_CLI_INCLUDES])
31DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS])
57 ErrUnknownCommand = 100,
58 ErrUnknownOption = 101,
59 ErrMissingFilename = 102,
60 ErrUnknownHelpTopic = 103,
61 ErrInternalError = 255,
64DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS])
66DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_ASSIGN])
72DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_ASSIGN])
74DOX_MARKER( [DOX_EXPR_TUT_CLI_CUSTOM_CAMP])
90 if( GetBootstrapState() == BootstrapPhases::PrepareResources) {
92 resourcePool->BootstrapBulk( ResourceCategory,
97 "Command line tool 'date'. V. {}.{} (in fact a sample application only)\n"
98 "(c) 2023-{} AWorx GmbH. Published under MIT License (Open Source).\n"
99 "For more information, see: https://alib.dev\n"
105 "1," "now" ",1" "," ","
106 "2," "file" ",1" ",filename" ","
107 "99," "help" ",1" ",topic" ) EOS
109 "Commands<",
A_CHAR(
"datesample::Commands::"),
111 "THlpCmdSht_now",
A_CHAR(
"Reports the actual date/time"),
112 "THlpCmdLng_now",
A_CHAR(
"Reports the actual date/time. May be omitted, as this is the\n"
113 "default if no command is given.") EOS
115 "THlpCmdSht_file",
A_CHAR(
"Returns the date/time of a file. "),
116 "THlpCmdLng_file",
A_CHAR(
"Returns the last modification date/time of a file.") EOS
118 "THlpCmdSht_help",
A_CHAR(
"Displays usage information. "),
119 "THlpCmdLng_help",
A_CHAR(
"Displays usage information. Can also be given as an "
120 "option '--help'.") EOS
126 "0," "format" ",1," "f," "=" ",1," ","
127 "99," "help" ",1," "h," "=" ",0," ) EOS
129 "Options<",
A_CHAR(
"datesample::Options::"),
131 "TOptUsg_format",
A_CHAR(
"--format[=]\"placholders\""),
132 "TOptHlp_format",
A_CHAR(
"Sets the output format. The format specification is given with\n"
133 "documentation of ALib method CalendarDateTime::Format, found here:\n"
134 "https://alib.dev/classalib_1_1strings_1_1util_1_1CalendarDateTime.html" ) ,
135 "TOptUsg_help" ,
A_CHAR(
"--help[[=]TOPIC]"),
136 "TOptHlp_help" ,
A_CHAR(
"Displays usage information.")
143 "0," "FILENAME" ",1," "" "," "=" "," ",-1" ",0" ","
144 "1," "TOPIC" ",1," "" "," "=" "," ",-1" ",1" ) EOS
146 "Parameters<",
A_CHAR(
"datesample::Parameters::"),
147 "THlpParSht_FILENAME",
A_CHAR(
"Mandatory parameter of command 'file."),
148 "THlpParLng_FILENAME",
A_CHAR(
"Denotes the file that is used for retrieving the modification date.\n"
149 "This parameter is mandatory to command file and has to be appended\n"
150 "to this command, separated by '='"),
151 "THlpParSht_TOPIC" ,
A_CHAR(
"Optional parameter of command (or option) 'help'."),
152 "THlpParLng_TOPIC" ,
A_CHAR(
"Denotes a specific toopic that the help command should be verbose about.")
159 "100," "ErrUnknownCommand" ",-1" ","
160 "101," "ErrUnknownOption" ",-1" ","
161 "102," "ErrMissingFilename" ",-1" ","
162 "103," "ErrUnknownHelpTopic" ",-1" ","
163 "255," "ErrInternalError" ",-1" ) EOS
165 "ExitCodes<",
A_CHAR(
"datesample::"),
167 "TExit0" ,
A_CHAR(
"Success (no error).")
168 ,
"TExit100" ,
A_CHAR(
"An unknown command was given. Valid commands are 'now' and 'file'")
169 ,
"TExit101" ,
A_CHAR(
"An unknown option was given. The only valid option is '--format='FORMATSPEC'.")
170 ,
"TExit102" ,
A_CHAR(
"Command 'file' given without a filename argument.")
171 ,
"TExit103" ,
A_CHAR(
"Command or option 'help' given without an unknown subtopic.")
172 ,
"TExit255" ,
A_CHAR(
"Unspecified internal error.")
176 "HlpCLIAppName",
A_CHAR(
"date"),
177 "HlpUsage" ,
A_CHAR(
"date [format=\"FORMATSPEC\" [now]|[file FILENAME]"),
178 "HlpHdlOpts" ,
A_CHAR(
"OPTIONS:" ),
179 "HlpHdlCmds" ,
A_CHAR(
"COMMANDS:" ),
180 "HlpHdlExtCds" ,
A_CHAR(
"EXIT CODES:" ),
181 "HlpHdlUsage" ,
A_CHAR(
"USAGE:" ),
182 "HlpHdlDscr" ,
A_CHAR(
"DESCRIPTION:" ),
183 "HlpHdlPDscr" ,
A_CHAR(
"PARAMETER DESCRIPTION:" ),
184 "HlpHdlTopic" ,
A_CHAR(
"Help on {} {!Q<>}:\n" ),
189 "This is a sample application provided with C++ library 'ALib'\n"
190 "to demonstrate the use of its module \"ALib CLI\"."
198 else if( GetBootstrapState() == BootstrapPhases::PrepareConfig )
200 enumrecords::bootstrap::Bootstrap<Commands >();
201 enumrecords::bootstrap::Bootstrap<Parameters >();
202 enumrecords::bootstrap::Bootstrap<Options >();
203 enumrecords::bootstrap::Bootstrap<ExitCodes >();
208 virtual void Shutdown( ShutdownPhases phase )
override
212DOX_MARKER( [DOX_EXPR_TUT_CLI_CUSTOM_CAMP])
214DOX_MARKER( [DOX_EXPR_TUT_CLI_CUSTOM_CAMP_SINGLETON])
216extern SampleCamp SAMPLE_CAMP;
217SampleCamp SAMPLE_CAMP;
218DOX_MARKER( [DOX_EXPR_TUT_CLI_CUSTOM_CAMP_SINGLETON])
220DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_ASSIGN2])
226DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_ASSIGN2])
228DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_FWDDECL])
231DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_FWDDECL])
233DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_MAIN])
239#include "ALib.Lang.CIFunctions.H"
241int main(
int argc,
const char **argv )
254 Enum result= ExitCodes::ErrInternalError;
265 buffer.
AddMarked( SAMPLE_CAMP.GetResource(
"AppInfo" ),
270 cli.AppInfo.Allocate(
cli.GetAllocator(), buffer.
Buffer);
273 cli.Init( &SAMPLE_CAMP );
276 cli.DefineParameters<
enum Parameters>();
277 cli.DefineCommands <
enum Commands >();
278 cli.DefineOptions <
enum Options >();
279 cli.DefineExitCodes <
enum ExitCodes >();
288 if(
cli.OptionArgsIgnored.size() )
290 result= ExitCodes::ErrUnknownOption;
291 std::cerr <<
"Error: Unknown option given \""
292 <<
cli.OptionArgsIgnored.front()
293 <<
"\"" << std::endl;
298 result= processCLI(
cli );
304 std::cerr << e.
Format() << std::endl;
308 catch(std::runtime_error& e)
310 result= ExitCodes::ErrInternalError;
311 std::cerr <<
"A runtime error occurred: " << e.what()<< std::endl;
319DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_MAIN])
322DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_PROCESS])
332 format <<
"yyyy-MM-dd HH:mm:ss";
335 Option* option=
cli.GetOption( Options::Format);
342 option=
cli.GetOption( Options::Help);
347 std::cerr <<
"Error: Unknown help Topic \""
350 <<
"Usage Information follows: " << std::endl << std::endl;
351 option->
Args.Clear();
355 std::cout << helpText.
Buffer << std::endl;
356 return ExitCodes::OK;
360 cli.ReadNextCommands();
361 if(
cli.CommandsParsed.size() == 0 )
364 if(
cli.ArgsLeft.size() > 0 )
366 std::cerr <<
"Error: Unknown command given \""
367 <<
cli.ArgStrings.at(std::size_t(*
cli.ArgsLeft.begin()))
368 <<
"\"" << std::endl;
369 return ExitCodes::ErrUnknownCommand;
376 std::cout << printBuffer << std::endl;
378 return ExitCodes::OK;
385 while ( (actCmd=
cli.NextCommand()) !=
nullptr )
389 if ( actCmdCode == Commands::Now )
394 else if ( actCmdCode == Commands::File )
399 std::cerr <<
"Error: no filename given with command 'file'" << std::endl;
402 return ExitCodes::ErrMissingFilename;
407 std::filesystem::path path( name.Terminate() );
408 dt.
Import( std::chrono::clock_cast<std::chrono::system_clock>(
409 std::filesystem::last_write_time( path ) ) ) ;
412 else if ( actCmdCode == Commands::Help )
416 std::cerr <<
"Error: Unknown help topic" << std::endl;
419 return ExitCodes::ErrUnknownHelpTopic;
421 std::cout << helpText.
Buffer << std::endl;
430 std::cout << printBuffer << std::endl;
432 return ExitCodes::OK;
434DOX_MARKER( [DOX_EXPR_TUT_CLI_ENUMS_PROCESS])
static ALIB_DLL bool GetHelp(CommandLine &cmdLine, Command *helpCmd, Option *helpOpt, Paragraphs &text)
static ALIB_DLL AString GetCommandUsageFormat(CommandLine &cmdLine, CommandDecl &commandDecl)
const Enum & Element() const
ALIB_DLL AString & Format(AString &target) const
ALIB_DLL Message & Back() const
ALIB_DLL AString & Format(Substring format, AString &target, lang::CurrentData targetData=lang::CurrentData::Keep) const
void Import(TTimePoint timePoint)
#define ALIB_ENUMS_ASSIGN_RECORD(TEnum, TRecord)
#define ALIB_LOCK_RECURSIVE_WITH(lock)
#define ALIB_RESOURCED_IN_MODULE(T, Camp, ResName)
@ Clear
Chooses to clear existing data.
@ Filename
Denotes the actual source file as the scope.
files::File File
Type alias in namespace alib.
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
LocalString< 4096 > String4K
Type alias name for TLocalString<character,4096>.
time::DateTime DateTime
Type alias in namespace alib.
void Shutdown(ShutdownPhases targetPhase, camp::Camp *targetCamp)
void BootstrapAddDefaultCamps()
format::Paragraphs Paragraphs
Type alias in namespace alib.
void Bootstrap(BootstrapPhases targetPhase, camp::Camp *targetCamp, int alibVersion, int alibRevision, TCompilationFlags compilationFlags)
strings::util::CalendarDateTime CalendarDateTime
Type alias in namespace alib.
boxing::Enum Enum
Type alias in namespace alib.
exceptions::Exception Exception
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
ALIB_DLL List< MonoAllocator, camp::Camp * > CAMPS
cli::CommandLine CommandLine
Type alias in namespace alib.
A command of a ALib CLI command line.
CommandDecl * Declaration
The underlying declaration.
List< MonoAllocator, Parameter *, Recycling::Shared > ParametersMandatory
Mandatory parameters parsed.
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.