|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- /*********************************************************************
- * C++ Base Application Object
- * Written by Jonathan A. Foster <jon@jfpossibilities.com
- * Started August 31st, 2020
- *********************************************************************/
- //#include <ostream>
- #include <iostream>
- #include <cstdlib>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include "cli.h"
-
-
-
- /*********************************************************************
- * cBaseApp
- *********************************************************************/
- cBaseApp *baseapp = 0;
-
-
-
- cBaseApp &cBaseApp::init(int argc, char **argv) {
- command_argc = argc;
- command_args = argv;
- }
-
-
-
- int cBaseApp::main() {
- int i, ct;
- char *p;
- bool switches = true;
-
- for(i=1; i<command_argc; i++) {
- if(switches && *command_args[i]=='-') { // switches!
- ct=0;
- if(command_args[i][1]=='-') { // long switches
- if(command_args[i][2]==0) { // --
- switches=false;
- continue;
- }
- p = command_args[i]+2;
- ct = do_switch(p);
-
- } else { // short switches
- for(p=command_args[i]+1; *p; p++) {
- if(ct) throw std::runtime_error(
- "Only the last switch in stacked short switches can require an argument"
- );
- ct = do_switch(p);
- }
- p--;
- }
-
- // switch arguments
- while(ct--) {
- if(++i>=command_argc) throw std::runtime_error(
- "Last switch requires more arguments"
- );
- do_switch_arg(p, command_args[i]);
- }
-
- } else // non-switch arguments
- do_arg(command_args[i]);
- }
- return ExitCode;
- }
-
-
-
- int cBaseApp::crash(const std::exception &e) {
- std::cerr << "Application crashed: " << e.what() << std::endl;
- return 216; // just a weird number hopefully not conflicting with anything else.
- }
|