Package: pikli
Pikli: Library For Making CLI AppsA simple python library to build command-line interfaces. Heavily inspired byCobra.Installingpip install pikliGetting StartedPikli is a command line parser. It parses the arguments provided on the command prompt & decides whether its a command or a flag or an argument for a command & acts accordingly. A command may have: Flags , Arguments & Sub Commands. In the following example:#main.pyimportpiklidefstart_server(arg):print("HTTP server running")root=pikli.Command(use="hello",short="hello is a cli app")serve=pikli.Command(use="serve",short="starts the http server",run=start_server)root.add_command(serve)root.execute()We have two commandsroot&serve.rootas its name suggests is the root command. This decision is made by providing a parent-child relationship. Its basically a tree like structure. And the command which sits at the top of the tree is the root. Theservecommand is made a sub/child command ofrootby theadd_commandmethod of theCommandclass.Theadd_commandmethod takes arbitrary amount of commands to add as asub command. Andexecutedoes exactly what it looks like, executes the command. Now onto the parameters provided while creating the objects:use: Determines the name of the command. Mandatory.short: A short description of the command.long: A long description of the command.run: The function which is triggered when theexecutemethod of a command is called. There is a thing to remember about therunfuntion. The function which is to be used as therunfunction(in this casestart_server),must have a single parameter which will be used as a list..FlagsFlags are extra options used with a command. For example:git commit -m "Initial Commit"here,gitis theroot command,commitis thesub command,-mis theflag& the string after that is its value. Now lets see a pikli example:importpiklidefstart_server(arg):print("HTTP server running on port:{}".format(pikli.get_int("port")))root=pikli.Command(use="hello",short="hello is a cli app")serve=pikli.Command(use="serve",short="starts the http server",run=start_server)serve.flags().intp("port","p",8000,"the port on which the server runs")root.add_command(serve)root.execute()Theflagsmethod of aCommandreturns theflagobject that handles every flag related activity for the command.intpis a method of that object which creates aninteger flag.There is alsostringp&boolp. The first parameter is the name of the flag(used in the long version), the second one is the usable name of the flag like,-p. The third parameter is the default value for the flag. There is no default value for the bool flag. Its False by default. And the fourth one should be obvious, a description of the flag. Now lets use everything we have seen so far:python main.py serve -p 8080orpython main.py serve --port=8080The output should be:HTTP server running on port: 8080Executing the serve command without thepflag will return the default value whenpikli.get_int("port")is called which is a pikli core function used for retrieving the value of an integer flag. Similarly there areget_str&get_boolto getstring&boolflag values.The Help FlagPikliprovides an automatic help flag generation & recognition. Whenever acommandwithout arunfunction is executed, thehelpflag will be executed autmatically. Or, it can be explicitly mentioned like any other flag like-hor--help. Try:python main.py serve --helpSimply runningpython main.pywill trigger its help flag as it has norunfunction. A help flag should display something similar:hello is a cli app


Usage:
	hello [args] [flags] [sub commands]


Available Commands:
serve            starts the http server


Flags:
-h, --help                Shows info regarding the commandThe Persistent FlagPikliprovides support forpersistent flags.Persistent flagsare like normalflagsexcept if you assign it to acommandit automatically gets assigned to every child it has upto the bottom of thecommandtree. So if apersistent flagis assigned to theroot commandthen everycommandwill get thatflag.importpiklidefstart_server(arg):ifpikli.get_bool("verbose"):print("showing details")print("HTTP server running on port:{}".format(pikli.get_int("port")))root=pikli.Command(use="hello",short="hello is a cli app")serve=pikli.Command(use="serve",short="starts the http server",run=start_server)serve.flags().intp("port","p",8000,"the port on which the server runs")root.add_command(serve)root.persistent_flags().boolp("verbose","v","shows details regarding the operation")root.execute()Here theverboseflagis assigned to theroot commandmaking this flag a global one. When assigningpersistent flags, don't forget to add all thesub commandsat first.ArgsApart fromsub commands&flagsnormal arguments can be used inpikli. All that is needed is thearglist that is used as the parameter of therunfunction. Lets see an example:importpiklidefgreet(arg):print("Hello{}, beef cheese delight rocks".format(arg[0]))root=pikli.Command(use="hello",short="hello is a greeting app",run=greet)root.persistent_flags().boolp("verbose","v","shows details regarding the operation")root.execute()Just keep the index order of theargumentsright. The index number of theargumentsdoesn't bother about theflags. For examplepython main.py -v "John Doe"pikliwill ignore the flags & count theargumentJohn Doeas index 0 & so on. The output should be:Hello John Doe, beef cheese delight rocksEnvLastly you can get the string or integer environmental variables usingpikli. The two functions for this areget_str_env&get_int_env:importpiklidefgreet(arg):print("Hello{}, beef cheese delight rocks".format(pikli.get_str_env("NAME")))root=pikli.Command(use="hello",short="hello is a greeting app",run=greet)root.execute()Run it like this:NAME="John Doe" python main.pyContributingTotally open to suggestions.See the contribution guideLicensePikli is licensed under theMIT License