]> git.armaanb.net Git - gen-shell.git/blobdiff - src/Sarge/README.md
added argument parsing with Sarge
[gen-shell.git] / src / Sarge / README.md
diff --git a/src/Sarge/README.md b/src/Sarge/README.md
new file mode 100644 (file)
index 0000000..7d69fc7
--- /dev/null
@@ -0,0 +1,207 @@
+# Sarge #\r
+\r
+Sarge is a simple and powerful command line argument parser, with the C++ version consisting out of <200 lines of well-commented C++ code, contained in a single class:\r
+\r
+\r
+       -------------------------------------------------------------------------------\r
+       Language                     files          blank        comment           code\r
+       -------------------------------------------------------------------------------\r
+       C++                              1             42             41            116\r
+       C/C++ Header                     1             12              7             35\r
+       -------------------------------------------------------------------------------\r
+       SUM:                             2             54             48            151\r
+       -------------------------------------------------------------------------------\r
+\r
\r
+\r
+Simply add the header file and source file to one's C++ project and use the class as in the project's test code:\r
+\r
+    #include "../src/sarge.h"\r
+       \r
+       #include <iostream>\r
+       \r
+       \r
+       int main(int argc, char** argv) {\r
+               Sarge sarge;\r
+               \r
+               sarge.setArgument("h", "help", "Get help.", false);\r
+               sarge.setArgument("k", "kittens", "K is for kittens. Everyone needs kittens in their life.", true);\r
+               sarge.setArgument("n", "number", "Gimme a number. Any number.", true);\r
+               sarge.setArgument("a", "apple", "Just an apple.", false);\r
+               sarge.setArgument("b", "bear", "Look, it's a bear.", false);\r
+               sarge.setArgument("", "snake", "Snakes only come in long form, there are no short snakes.", false);\r
+               sarge.setDescription("Sarge command line argument parsing testing app. For demonstration purposes and testing.");\r
+               sarge.setUsage("sarge_test <options>");\r
+               \r
+               if (!sarge.parseArguments(argc, argv)) {\r
+                       std::cerr << "Couldn't parse arguments..." << std::endl;\r
+                       return 1;\r
+               }\r
+               \r
+               std::cout << "Number of flags found: " << sarge.flagCount() << std::endl;\r
+               \r
+               if (sarge.exists("help")) {\r
+                       sarge.printHelp();\r
+               }\r
+               else {\r
+                       std::cout << "No help requested..." << std::endl;\r
+               }\r
+               \r
+               std::string kittens;\r
+               if (sarge.getFlag("kittens", kittens)) {\r
+                       std::cout << "Got kittens: " << kittens << std::endl;\r
+               }\r
+\r
+               std::string textarg;\r
+               if (sarge.getTextArgument(0, textarg)) {\r
+                       std::cout << "Got text argument: " << textarg << std::endl;\r
+               }\r
+               \r
+               return 0;\r
+       }\r
+\r
+Only dependencies are a reasonably modern C++ compiler, capable of supporting at least C++11 (STL datastructure improvements).\r
+\r
+## API ##\r
+\r
+       void setArgument(std::string arg_short, std::string arg_long, std::string desc, bool hasVal);\r
+       void setArguments(std::vector<Argument> args);\r
+       void setDescription(std::string desc);\r
+       void setUsage(std::string use);\r
+       bool parseArguments(int argc, char** argv);\r
+       bool getFlag(std::string arg_flag, std::string &arg_value);\r
+       bool exists(std::string arg_flag);\r
+       bool getTextArgument(uint32_t index, std::string &value);\r
+       void printHelp();\r
+       int flagCount();\r
+       std::string executableName();\r
+\r
+## Supported flag types ##\r
+\r
+Sarge supports both short and long options, prefixed by one or two dashes ('-') respectively. The short option can be left empty, which will only enable the long option.\r
+\r
+Short option: `-h`.\r
+\r
+Long option: `--help`.\r
+\r
+Options can optionally be followed by a value string. This has to be noted when registering the flag with Sarge in one's code. \r
+\r
+It's also supported to supply multiple short options combined to Sarge, e.g.: `-hnklm`. Important here is that options which require a value to follow them have to always be at the end of such a chain.\r
+\r
+String without flag associated with them are made available using the `getTextArgument()` method after parsing. These arguments are only allowed to exist after the flags section.\r
+\r
+## Compiling the test application ##\r
+\r
+A Makefile has been added to the root of the project. Simply execute `make` in the folder to compile the test binary into the `bin/` folder. Execute `bin/sarge_test` to get the following output:\r
+\r
+       # ./bin/sarge_test.exe -hk Mew\r
+       Number of flags found: 2\r
+       \r
+       Sarge command line argument parsing testing app. For demonstration purposes and testing.\r
+       \r
+       Usage:\r
+               sarge_test <options>\r
+       \r
+       Options:\r
+       -h, --help      Get help.\r
+       -k, --kittens   K is for kittens. Everyone needs kittens in their life.\r
+       -n, --number    Gimme a number. Any number.\r
+       -a, --apple     Just an apple.\r
+       -b, --bear      Look, it's a bear.\r
+           --snake     Snakes only come in long form, there are no short snakes.\r
+\r
+\r
+As you can see, no kittens were harmed in the production of this code :)\r
+\r
+# Ada version #\r
+\r
+The Ada version of Sarge (found in the `ada/` folder) is pretty much a straight port of the C++ version. It consists out of a single package (Sarge), with <200 lines of code. \r
+\r
+       -------------------------------------------------------------------------------\r
+       Language                     files          blank        comment           code\r
+       -------------------------------------------------------------------------------\r
+       Ada                              2             58             44            197\r
+       -------------------------------------------------------------------------------\r
+       SUM:                             2             58             44            197\r
+       -------------------------------------------------------------------------------\r
+\r
+\r
+Its biggest limitation compared to the C++ version at this point is that one cannot use multiple instances of Sarge since the relevant data structures are part of the package. This should not pose any issues in the average usage scenario, however.\r
+\r
+\r
+## API ##\r
+\r
+       procedure setArgument(arg_short: in Unbounded_String; arg_long: in Unbounded_String; desc: in Unbounded_String; hasVal: in boolean);\r
+       procedure setDescription(desc: in Unbounded_String);\r
+       procedure setUsage(usage: in Unbounded_String);\r
+       function parseArguments return boolean;\r
+       function getFlag(arg_flag: in Unbounded_String; arg_value: out Unbounded_String) return boolean;\r
+       function exists(arg_flag: in Unbounded_String) return boolean;\r
+       function getTextArgument(index: in Integer; value: out Unbounded_String) return boolean;\r
+       procedure printHelp;\r
+       function flagCount return integer;\r
+       function executableName return Unbounded_String;\r
+\r
+## Example ##\r
+\r
+The test application has also been ported from the C++ version, showing the use of the package:\r
+\r
+       with Sarge;\r
+       with Ada.Text_IO;\r
+       use Ada.Text_IO;\r
+       with Ada.Strings.Unbounded;\r
+       use Ada.Strings.Unbounded;\r
+       with Ada.Strings.Unbounded.Text_IO;\r
+       use Ada.Strings.Unbounded.Text_IO;\r
+\r
+\r
+       procedure Sarge_Test is\r
+\r
+       function "+"(S : in String) return Unbounded_String renames Ada.Strings.Unbounded.To_Unbounded_String;\r
+\r
+       kittens: Unbounded_String;\r
+       number: Unbounded_String;\r
+\r
+       begin\r
+          Sarge.setArgument(+"h", +"help", +"Get help.", False);\r
+               Sarge.setArgument(+"k", +"kittens", +"K is for kittens. Everyone needs kittens in their life.", True);\r
+               Sarge.setArgument(+"n", +"number", +"Gimme a number. Any number.", True);\r
+               Sarge.setArgument(+"a", +"apple", +"Just an apple.", False);\r
+               Sarge.setArgument(+"b", +"bear", +"Look, it's a bear.", False);\r
+               Sarge.setArgument(+"", +"snake", +"Snakes only come in long form, there are no short snakes.", False);\r
+                       Sarge.setDescription(+"Sarge command line argument parsing testing app. For demonstration purposes and testing.");\r
+               Sarge.setUsage(+"sarge_test <options>");\r
+\r
+               if Sarge.parseArguments /= True then\r
+                       put_line("Couldn't parse arguments...");\r
+                       return;\r
+               end if;\r
+\r
+               put_line("Number of flags found: " & Sarge.flagCount'Image);\r
+\r
+               if Sarge.exists(+"help") /= False then\r
+                       Sarge.printHelp;\r
+               else\r
+                       put_line("No help requested...");\r
+               end if;\r
+\r
+               -- Read out Kittens and Number.\r
+               if Sarge.getFlag(+"kittens", kittens) = True then\r
+                       put_line("Got kittens: " & kittens);\r
+               end if;\r
+\r
+               if Sarge.getFlag(+"number", number) = True then\r
+                       put_line("Got number: " & number);\r
+               end if;\r
+\r
+               if Sarge.getTextArgument(0, textarg) = True then\r
+                       put_line("Got text argument: " & textarg);\r
+               end if;\r
+\r
+       end Sarge_Test; \r
+\r
+The Makefile in the `ada` folder should be used when compiling the test application. The Gnat Programming Studio project file is currently not maintained. \r
+\r
+The Sarge package is found in the `ada/src` folder. One can use it directly from there by including it one's project, or copying it into the project's source tree, depending on one's requirements.\r
+\r
+\r