hi my name is noah gift and i'm going to talk to you about how to take a python function
and map it to a command line tool with as little code as possible we're going to use
the python fire library which is originated from google and let's go ahead and get started
let's map a python function to a command line tool first let's go ahead and define our function we'll
call this function fruit and this fruit function will be something later we will map to a command
line tool library the python library fire so here we have a fruit generator that accepts
fruits and then inside of here i'll go ahead and import my random library which i'll say from ram
random import choices great now that i've got that i can generate some kind of logic and the
logic that i will generate will be something that will just return back something useful in this
case it will be choices of fruits so essentially randomly choose fruits to return back to the user
so this is a very simple function here not a lot of work but let's go ahead and cue this up
so i'll add a doc string here which returns back random fruit and then next what i'll
do is put my logic inside of here as well and what i'll do is i'll i'll print out my
result here so i can just test out the basic script here which will print out the fruit
generator and then i'll put in let's say a list of fruits and then it'll pick one of those fruits to
return back so very simple random function here i like to do build random functions because they're
nice to test with since i'll get different values every time i test it there we go hello peach hello
apple right i'm randomly choosing one of those fruits so now that i've got this thing wired up
really the next step here is to figure out how to make this thing map to a command line tool library
and what i'll need to do is figure out essentially how to map this function directly into this python
fire command line tool so let's go ahead and take a look at how that would work next year
okay now we have python fire you can see it's a simple way to generate command line tools from
just any python object so one of the most efficient possible ways you could build a
chameleon tool you can see here's an example i would just have a hello function and then if i
import fire i can just fire dot fire hello right this in some sense is the the shortest distance
between two points is taking uh i can't imagine actually a quicker way to write command line tools
than a library like this and so let's go ahead and do that ourselves so i'm going to first install it
in my requirements file so i'll say fire i have a make file here so i can just type in
make it stall great that works and then next what i can do is once i've got this thing
wired up is set this up inside of my my script here so i can turn a script basically into a
command line tool very very easily so we'll say right here import fire that's great
let's go ahead and do that import fire and then next here i'll need to just follow this
bottom syntax here and this is something that confuses many people is when you build a script
in python you do need to put if under under name under under equals main and the reason for this
is to tell the python interpreter that this is actually meant to be invoked only as a script
and so if i import this it wouldn't actually invoke the code it only will run if i run it
as a script and and all command line tools will use this syntax so that it it differentiates
it so when you see this block here that's all it means now if i go through here and i say fire.fire
this is where i would actually go through here and put in the fruit generator code
in this case i'm actually trying this out to show how a mistake would be made in this case uh oh
wait what are we doing here ah right uh it gives me a an error because of the fact that i actually
am not calling it a function i'm just calling a variable that doesn't exist aha here's the problem
and even if i go through and i do the help menu it's still going to have the same issue and that
in this particular scenario here i'm not actually able to invoke a function so if i change it though
to fruit generator and then we go through and we do help aha here we here we see that in fact i get
all these fancy things here we get uh the fact that it uh has a help menu it tells me exactly
what it takes so this is really a great interface here for me to build up the next piece of logic
okay now that i have tried things out here and i know that i need to tweak things a little bit what
i'm going to do is i'm going to add inside of here a little bit more logic and what i'll do is i'll
put in a an array of different fruits because the way i had the code before earlier it was a little
difficult to parse a list and so i'll put the list inside of this function next what i'll do is
i will make it so that i will allow the user to append something to this internal list and then
it will randomly choose from this internal list in our command line tool so this makes it a little
bit more sophisticated and makes it easier to use and that way i don't have to try to figure
out how to parse a list which is not necessarily conducive to the bash terminal so we'll go ahead
and now try this out and i'll put in a name of a fruit in this case i say pear and it says up
there we go so it randomly picked a cherry but i did append pair to the internal list now this
is a little bit difficult to see what's happening though and so what it may be helpful to do is to
add a little bit of logic in here that just shows what it is i'm putting into the chameleon tool and
then also what fruit is actually randomly selected so without this logic it might be
confusing to the user to to really understand what's happening so we'll say you added this fruit
and i'll use an f string here to put this fruit inside next up i'll go ahead and i'll say here's
just here's the random fruit that was chosen there we go randomly selecting from of the fruits
great and i'll put this as well inside of an f-string
and and this will then go and and return back
more information to the user of this command line tool python hello pair there we go you're adding
this fruit pair randomly selecting from all of the fruits you can see that the pair that i passed in
was actually returned to the command line so we've actually made a much more useful command line tool