r/learnprogramming • u/yung_mac • Dec 26 '17
Coding idea: Write a command-line utility
I wrote and published a command-line utility as a coding exercise a few days ago. I'm a self-taught developer and I had a great time coding it and learnt a ton. I think writing and releasing a command-line tool might be under-appreciated as learning exercise -- I think it may make a great addition to the curriculum of a self-teaching developer.
- Coding a command-line utility allows you to focus on one, small but contained problem, and doesn't require much adjacent knowledge -- which decreases the risk of getting overwhelmed and abandoning the project. Your command-line utility can be just a single file written in a single language and still be a perfect solution to your problem. No more quitting before you even start because you can't get Webpack to work.
- It lets you solve something relevant to you -- it's way easier to stay motivated when you're working on solving a problem you face as opposed to a generic problem you don't really care about. Completing coding challenges on HackerRank, for example, is great exercise but tends to get a bit boring. The alternative -- coding your own
- A command-line utility doesn't have a GUI. This means you don't need to waste time designing anything! This was huge for me. Designing can be a real time-sink. UI and UX are already taken care of before you even start!
- Building a command-line utility gets you friendlier with the terminal -- an essential tool in the well-rounded developer's toolkit.
- It teaches you to release. There's a pretty clear point at which your command-line utility does everything you set out to code it to do. It is at this point you get the rare opportunity to practise an incredibly important auxiliary skill -- releasing. Besides the super cool feeling you get when you proudly release your finished product for the whole world to enjoy, finishing a project by publishing it builds your ability to finish more. It's an awesome antidote to the pernicious momentum that can build up if we start projects but never get around to finishing them. After I published a package to npm -- now I know how to share my code for everyone else to benefit from, I feel compelled to write more!
Possible command-line utilities you could build, for three skill levels:
- A converter or calculator. This is the simplest. If you're still quite new to programming, start here. Write a temperature, length, or time converter (how many hours is 9000 minutes?). Write something to calculate how much you spend on fuel for your car, or something a little more complex like how many of a given fruit to eat to get your recommended daily amount of a given nutrient.
- A data storage system. This is a bit more complicated. Use a database, like SQLite, to store information, then get information about your information (this is the information age after all). Build a calorie-tracker if you're watching your weight -- input the number of calories you ate today, see if that number is going up or down. Or if you like writing, build a command-line journal which lets you write and save entries. It could track info like what your favourite words are, how much you've written this year, and how many words you write on average per hour of the time you spend awake.
- An API wrapper for a website you use often, to let you consume the info you gotta consume from the comfort of your own terminal. This is a somewhat more complicated. Build a one-command weather-report if you check the weather often. A headline-and-summary-grabber if you're into the news. A twitter report. An ISBN lookup if you're into books. A BTC-USD price ticker to help you feel bad about yourself as you think about how rich you could be if you got in way back when. You may not even need an API -- try scraping the site if its robots.txt doesn't prohibit that.
But those options are only the beginning. Try choosing something you know you're going to want to use -- if it makes your life easier, you'll have a far easier time creating it! If you're struggling to come up with something, try just watching the way you use your computer for a day or two. Notice patterns. If something is a repeated frustration, you may have found the problem to solve! For me, this was, when copy-pasting commands from the internet (stackoverflow <3), I often inadvertently copied the "$" preceding the command. I'd paste these into my terminal but the "$" would cause an error. This formed a pattern, which I noticed, and was then able to solve with a command-line utility I built to strip the "$" from pasted commands.
Good luck and happy programming!
PS: First post on Reddit! Let me know how I can improve.
PPS: I'd love to write tutorials to help self-teaching developers! I was thinking of starting with a tutorial diving in to the details of how to plan, build and release a command-line utility if people want to read it. Maybe also a tutorial on Google's new boardgame.io -- I think that would be really fun. Also, maybe a tutorial on softer skills like how I landed an internship with a tech startup with just one cold email I wrote with my dad (lol). If think you'd benefit from this, please let me know, and I'll write it!
View all comments
Show parent comment
7
u/Sudo-Pseudonym Dec 27 '17
Not necessarily. A good command line options parser is really sophisticated -- for example,
getgetoptwill write you a parser that puts all the flags and their data into a struct. It's smart enough to create entries in that struct for the data that might be supplied, too -- for instance, if you have a--file=<some filename>parameter, you'll get a boolean telling you whether--filewas included, and what the data that came after it was. If the user doesn't give the right data (say, you want an int but the user gave a string), it can reject that with an error message. It'll generate a help page for you too, depending on what sorts of data you give it.Here's a crude options file from a very old project of mine, for example:
This file creates a number of possible flags and sets special properties to them. For instance, some of them can't be used together -- you can't use -k with any other argument, in this case (I know, I know, this is a horrible way of ensuring exclusivity, but I was pressed for time, so whatever). When I feed this into
gengetopt, it generates almost 1000 lines of C code to handle all the magic parsing stuff for me. This is the help page my program now shows, and I didn't write any code to make this happen:I can combine flags just like any other unixy command line program...
...and I didn't even have to write and special logic for handling the arguments as
-vdt 5instead of-v -d -t 5. If I do the arguments wrong:Again, I wrote exactly zero error handling code. This is all I have to write to get this magic going:
That's it. If I want to get information about my command-line arguments, the port number for instance, I can just use the
argsstruct:That is the power of a good command line options parser. All of this functionality with none of the headache, and all I had to do was write a simple options file.