 Thanks everybody. Welcome to my talk building nice command line interfaces. I look by on the standard library I am Patrick Mubauer. I'm a back-end developer at Blue Yonder. I'm working in Kalsway And this is my third Europe Python now, but the first time as a speaker I'm pretty excited being able to speak here today and and what I want to talk about our command line interfaces and I want to show you a Little demo in the beginning I wrote a It's called brewery. I thought I'm originally from the various or German guy from a very I do something with beer So this is a typical help screen. I think everyone here knows What a command line interface and such a help screen is so you see a usage line You have a little description You see you have some options and of course some sub commands And yeah, you can call these sub commands. You have separated head pages for your sub commands. This would look like this so Here we have a list command you can Here we can see we are able to list some of the beers so it's would like This Okay Yeah, and these are these are actual actual beers from the Augustina Bray Famous brewery from Munich And we can also list We have seen we have some other options. We can list our own beers don't have any by now, but we can Buy us up a beer. So this So let's look what the buy command looks like So we have an option name and an option count Let's use this Let's buy a six pack of beer So now we have six balls of pills Yeah, so just that's just as a short introduction. So you get the idea what the command line in the face is if you don't know already and what I want to talk about is if you are New to Python or programming you might wonder how to start so I As I started programming I ended up Passing sys.arquee by hand and I have seen others doing the same And so this are key is just a list of the command line arguments and if you try to pass that by yourself you end up with pretty ugly code so or you Start writing your own parser, but yeah, they're already there are already some Like get up Get up is an implementation Yeah, well, it's the implementation for Python, but it's actually from C And if you are familiar with the C programming language and have used get up It might be a good fit for you, but I don't recommend to use it. It's not really nice so if you want to stick to the Standard library you might find op pass or arc pass more pleasant I have to say op pass is deprecated by now arc pass is the newer one and Yeah arc pass. I think it's okay. It's does a job. It's pretty straightforward, but If you if your application grows and you have multiple sub commands you always have to create all these sub parsers and Yeah, the code starts growing Not that nice. So If you think so too, you might be wondering if there's more And yeah, there is so I want to show you today three libraries which are out there Click dock opt and cliff The first one is click. It's a project Created by Armin Ronacher. I think a lot of you know him. He's quite popular in the Python community She has created for example flask and ginger And and he was also not that happy with the solutions already out there, so he decided to write his own and click was born and Click is a decorator implementation. So you use decorators to implement Mark your functions as commands for example And click is highly configurable and it also comes with good defaults So and doc opt has a completely different approach For doc opt you have seen this help screens for doc up you just write these help screens and then You already have your parser doc opt will pass this doc string and Then you get your arguments as a dictionary So the third one is cliff. It's from a developer from OpenStack and cliff is more like a framework for multi Level commands something like get for example where you have just get add get push and so on and for its sub commands it uses Setup tools entry points, so you define an entry point in your setup pie and then Cliff will find it and yeah, I will show you that later on and it also comes with output format us I think I have a little demo Better I show you that later So let's start with click. How does it look like? a Minimal example would look like this you just have a function and decorated with the command decorator and Yeah, you're done that then you could call your script and you already would get a help message looking like that Which is the starting point for doc opt so in doc opt you start writing this help message Then use the doc opt function put the stock string in it and get back the dictionary of the past arguments and options So for cliff, this is a little bit more cold But actually it's not that complicated. There are two important things here. It's this app class and the command manager, so you have to subclass from app and then you can You have to pass a command manager to it and this command manager is Yeah, the command manager says how subcommands should be found and and the default command manager here and finds Subcommands via setup tool entry points like I already said so it looks for set up entry points and in the cliff brewery Cliff dot brewery entry point names So subcommands for click You have another decorator called group. So now you say your run Function should be a group and then you use this group to decorate other functions as Command as subcommands which belong then to this group It's just these two lines of code and you have your first subcommand so in doc opt It's a bit more complicated if you want to have separate help messages For each subcommand because you could what you could do using subcommands is just write multiple usage lines where you can Say instead of command you have list by and so on But then you would not have separate help messages So what you can do is you can implement your subcommand in another module Write your doc string there and then So check here if the command you use this for example list import the module and then put its doc string to doc opt as using doc opt with subcommands is Not really nice Like you can see probably So and now cliff Here in your setup pie you have just entry points key words argument and Now you say Again this cliff dot brewery entry point group and To find a subject to define a subcommand you say for example list equals brewery commands, which is the brewery package in then the commands module and In the commands module is a class called your list command Which would look something like this so you just subclass from cliff command And then you would have to overload the take action function the take action function Already gets and past the past arguments as arguments and these are an arc pass namespace object, so Cliff uses arc pass internally So next Yeah, if you want to use You probably want to use options and arguments How in click it's again just a decorator you have an option decorator So here we say we want to add in debug option to our run group And we also say it should be a flag So it's just a boolean value in the end And the same for arguments you have an extra argument decorator Here we say we want a filter argument and this is then are also passed to the list function So in doc opt again, it's just writing your options and arguments in the doc string For example here if you have a subcommand list Which gets this optional filter argument Optional in such a help message are these brackets And here's also an example how such Dictionary would look like you what you get from the doc opt the function So Can in cliff you would have to overload to get pass a method There you always have to retrieve the parser Which is an arc pass argument parser and Then They are just used the add argument method to add The filter argument so here the in get parser. It's just a pass Mmm Okay, another feature or perhaps you don't want only want optional arguments. Perhaps you want repeating arguments In click you can could achieve this with the keyword argument n arcs in the arc in the argument decorator So if you use minus one for example, this means you Can put an infinite number of Filter arguments to your command and filter would then be a tuple of strings not just a string In doc opt again just documenting it So in such a help screen repeating is are these ellipses these three points and And the filter in the arcs dictionary would be a list okay arc pass has Also an n arcs keyword argument, but here. It's not a minus one. It's an asterix and Yeah, that's all so Another thing defaults so you don't want to Specify a value to every option all the time so for example If we for our by command we could say if we don't specify a number of bottles We want to buy and just buy one so This is done and click by a deep by the default keyword argument and you also have Required argument if you really want to say this option should be given a specified in doc opt you have to add This default one to at the end of the option help string and Same for required And yeah, that's all basically For arc pass again, it's the same like for click Just the required and default arguments keyword arguments One difference here is that click Uses If you specify a default value click uses the type of the value as default as yeah type so if you would Use count with something else than an integer you would get click would raise an error and So what you Probably also want is type support to validate your arguments and options so in click you have a type keyword argument and Click also has nice utilities for example custom types Here I used an int range as type because Buying zero or less balls would be nonsense anyway. So I said it should be between 1 and infinity And doc opt does not have any types. It's just everything is a string or bool and You have to do the checking all by yourself and For Arc pass you also have this type keyword argument. It's working best read the same like in click Okay, so another feature if you Want to automate things for example, you might want to use environment variables and In click again, it's just another keyword argument. So you specify an n far and In the example you can see If you don't have this n far set already the default value would be used, but if you set This n far then the n far is used And doc opt again does not have support for environment variables and And also Arc pass does not have built-in support, but you can do this little trick So you just set a default value and use the n viral dictionary And yeah, then you have basically the same effect like in click You probably also want to test your stuff if it's and Click this is really nice because click Comes with an extra testing module and in this testing module you have a key runner and If you you with this key runner you can just invoke your commands and Then you get back and result object and this result object then holds the exit code and the output so Testing what command returns is really easy using other stuff like arc pass and so on I I don't have a really good Pattern, so if you have one I would be glad if you come to me It's what you find in the internet is always the same. You just call your function and then Probably some people use a sub-process the sub-process module to call just Your script and then pass the output and so on but that just does not seem right what I Did here is I use hi test for testing and hi test has this capsis fixture and If you use this fixture it captures the output and Then you can test at the output in a similar way than to this click solution Okay so one last thing for cliff Cliff has I already mentioned these output formators and and Cliff has special sub commands For some of this list as just a sub subclass from command and I Probably show you Best I show you this so here's Cliff implementation you also have a list command and If you use this Lister class I showed you Then you would get This ugly output, so it's a table It's really awful, okay, but you have this extra option Which comes with cliff you can say Want it to be Okay, the resolution is not really nice here, but you can see you get a nice table Just by doing nothing By yourself basically What you have to do is you have to structure the data you want to show in this table, so it's just one header row and Then multiple rows you want to show and then Yeah, that's all so One other thing I Cliff is pretty cool when using plugins so Here we can see we don't have a drink command, which is really sad So we can Just go well think of this brewery drink like a plugin and Here we have This entry point defined so we have here drink command in this plugin and if we install it now Yeah, no we have a drink command nice Yeah, and sorry My favorite is click. It's really nice working with click. You have a lot of utilities. It's very robust It's just fun to work with and Doc-Opt is nice if you are already familiar with these help messages and you really want to write nice documentation and so on but for I think for bigger Applications is not that good and Yeah, Cliff. I really like these output formators and cliff. It's really easy to get a nice table or what? I oh, sorry what I have not shown you is you already you also have multiple formators For example, you can say you want the output as CSV or There are also extensions to get chasen or yaml And you can also specify the columns you want to show So if you just want the beer names and the descriptions Then Yeah, you would just have two columns so and I think We are out of time and that's the end So thanks for your attention if you have questions Just ask or ask at the coffee. Yeah Well, thanks for your talk One common usage in options is to have the long option with hyphen hyphen name and the short one with maybe hyphen N I don't recall seeing any particular examples about that, but I assume all of these tools would support that easily So what you mean is you want to have something Like this and this and yeah, sure could use either one Yeah, yeah, you were the same thing Yeah, well, you can do that in all three solutions. So okay, great. Thanks. Yeah More questions Hi, I'm just curious. Have you seen the Google's flux parsing library? I think it's called Python g-flux and if you've seen what's your opinion about it No, okay Do they have support for colors and the output Sorry, I do they have support for colors and the output support for what color for color. Oh, yeah so I Click has This echo function. I used it on one of my slides. I think it's basically Something is something like the print function, but it has a lot of stuff Internally to handle unicode for you on different platforms and so on and it also has a keyboard argument for colors and Yeah, then would you would get colors and I think in doc opt and Cliff you'd or a pass you don't have this built in so I Would like a tool that reads all these complex definition from a configuration file And not do them in the code in line, which is the best one to do that Or maybe it's not an option. Okay. You mean for doc opt or what for each of the tool I don't want to write long lines of codes I want to be able to add a function go to a YAML file and add a new command and and be done with it Sorry, I don't know if Might be possible somehow, but I don't know More questions Thanks. Thanks for the talk. Do you have any good solutions for interactive command line interface? Interactive And what cliff? Has Interactive built in I think do you mean something like that? but That's the only thing I know I don't know if there's something else or for click Sorry Yeah, so I actually have an answer to that So we use click and cookie cutter for the prompting and they use interface So click is actually capable of doing very very great interactive command line So It should be just click prompt and then you have different options so you can do choices and such so Yeah tomorrow at quarter to 12 my talk, I will talk about this as well Yeah, yeah, I got the question wrong But you have this prompt a click prompt and then you get just prompted for an option Is it that what you mean? Yes. Yeah a click has it more questions I do have one last one then and In your last projects, which one of these tools did you use and I personally use Most of time click Okay, but cliff is actually quite new to me too And but I really like these output format stuffs So if I have a project where I could need this Just creating CSV file out of some other data or so then I really I think it's a really good fit Okay, and I think we are done for any more questions Anyway, so we will let you go to the coffee break and thank you again Patrick