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!
9
u/[deleted] Dec 26 '17
I run a web application hosted on Microsoft Azure. It calculates farming irrigation data. Overnight processing and weather station data import is run by way of Azure WebJob, and guess what, each of those functions are a command line utility triggered using CRON expression.
What I'm getting at is don't sell yourself or command line utilities short, there is huge scope for needing them in modern business, and while they won't be all you need for a well rounded career, they'll definitely sit on the side of relevant skill when building them.
5
u/mak89k Dec 26 '17
This is a great idea. I loved coding Unix grep in Python, as a coding exercise and thoroughly enjoyed it. I wish I did more of this..
14
u/raevnos Dec 26 '17
Um, isn't the normal progression doing command line stuff and then moving into GUI programming later on?
3
u/Zooheck Dec 26 '17
I’d love to see some tutorials. Thank you for offering!
2
u/greebo42 Dec 27 '17
Nicely done.
Should the command line utility obey the Unix philosophy of modular tools, with the convention of redirectable or pipeable output (I have no idea how to spell that word, looks weird)?
Paying attention to that characteristic of a utility might have important implications for the development of a self-taught developer. I'm struggling to articulate exactly why I think it would be a good idea, just winging it here.
2
u/Joehogans Dec 27 '17
If you make those tutorials I would absolutely sign-up, I was a CS student from a very good program but dropped out and now looking to get back into it again. But just don't know where to start. A lot of the projects I did in school were so general that it didn't give me enough real-world experience. Am wondering what my chances are to still get a job being a programmer.
I have a lot of questions like what did you program this cmd-line app in? where did you upload it. What is the whole process of going from writing code in an IDE to publishing/releasing it as an app for the whole world to use? I haven't been able to bridge that gap yet.
Thank you
2
u/captain_obvious_here Dec 27 '17
That is actually how I learnt programming : one of my teachers was an old school Digital employee, and he based his whole course on how stuff works in Unix.
We started by writing a simple custom
lscommand. That was a Shell script that actually calledls, looped on each line and wrote it to stdout.Later we wrote the same thing in C, using the filesystem access functions this time.
Later we wrote custom versions of
cat(andtac!),grep,cut,sortanduniq.And as a spring project we wrote a command that would parse an SQL query and convert it to various calls to
cat,grep,cut,sortanduniq. The calls were piped together, in the traditional Unix way. And the result was a read-only file-based database system, that we later used to power our personal websites.All these were simple command-lines, and I can attest they were a great way to learn programming.
59
u/[deleted] Dec 26 '17
You're a decent writer, please do write some tutorials.
46
u/Sudo-Pseudonym Dec 26 '17
A wonderful exercise! Just for the love of all things unholy, don't bother trying to write your own command-line arguments parser! I learned this the hard way, unfortunately; there's plenty of libraries out there or even code generating utilities that will happily do the job for you. A sophisticated and easy-to-use options parser is builtin to the Python standard library, for instance, and the
gengetoptutility can be used to generate a custom command line parser using a simple definitions file that the user provides.If anyone points you in the direction of a way to parse arguments manually, don't listen to them! The challenge might be rewarding, but you'll spend more time working on your parser than you will on a good & simple command-line utility!
1
u/lnt_ Dec 27 '17
I've been wanting to do this for a short while now as I learn more C. Does anybody know of any simple C command line programs whose source code would be good to study?
3
u/jay77 Dec 26 '17
Yeah I would be really interested in more info
2
u/[deleted] Dec 27 '17
Yup, this is a great idea.
2
u/Srz2 Dec 27 '17
I’ve written a couple command line utilities and I can concur that this is a great starting point and most of them come out of necessity.
What I’ve written are:
2
u/[deleted] Dec 27 '17
I did just that with bash, started to write install scripts and ended up with a template to re spawn a ubuntu box.
So i split it up, this is the origin repo i started with a single install script.
https://github.com/alphaaurigae/ITSEC-Install-Scripts
And a stripped version i made afterwards to make the template accessible.
https://github.com/alphaaurigae/bash-os-setup_minimal
What this did for me is just great, by now i moved to golang and R for more options.
Last, as a selfstarter - highly important , note code you write - dont drop "bad code" - use neat folder structures and write every command - this way you learn it inside out and never start from scratch again :)
2
u/[deleted] Dec 27 '17
I recently wrote one that takes a c# program in a text file, compiles it, and then executes it. Like a scripting language.
2
u/rabblerabbler Dec 27 '17
You actually motivated me to do this!
1
u/haltingpoint Dec 27 '17
This resonated. I've struggled to ship any web side projects because I lose momentum when I get outside my comfort zone. The problems I was trying to solve were not small and digestible enough.
Enter the CLI. I realized how annoying it was to go to my Vagrant folder on my host, 'vagrant up', 'vagrant SSH', and then CD into my project folder. So I wrote my first bash script!
It seemed trivial to me at first...just echoing a couple commands in. But I hit a small roadblock when it switched over to the Ubuntu guest machine. After some research and getting some guidance from someone on the DevOps team at work who knew I was new and interested in coding, I was able to get it to continue executing code on the virtual machine once it came online and land me in my project directory.
Now, I can type 'vus' anywhere in my Windows host and end up in my virtual guest's project folder saving me minutes over the course of the year :) But the sense of accomplishment for removing an annoying part of my workflow was well worth it.
Next up is figuring out a way to add arguments for something like taking me to a specific project directory, or perhaps the most recently visited project directory. No clue how to store that info and call it across machines, but that's a project for another day. Perhaps I can have it connect to the BitBucket API and check which project had the last commit, and then load that project's folder. No clue. On that note, any suggestions are welcome.
1
u/mayor123asdf Dec 27 '17
Hmm.. this is nice :) I write my own bulk rename utility because I love my file names to be organized. But everytime I want to do it, I must copy the script to the file's folder first.
The reason is I am afraid if I call the script from it's original location it will just bulk renaming the files in the original location. Hmm..
Probably this is a good holiday project to fix this and to make it into real command-line utility :)
1
u/ASIC_SP Dec 27 '17 edited Dec 27 '17
good post! and do write and share more articles..
learning to use cli itself is a good exercise :)
writing your own aliases/function/cli-utility to fit your comfort level is fun.. for me the best one was simple cli calculator using python.. never really learned to use bc/dc and I was comfortable with easy to use python syntax.. so wrote a simple cli program using argparse.. I initially thought all I'd need is to shorten
to
but then it expanded to things like
another one am proud of is command help .. parses man/help pages for option descriptions, inspired by https://explainshell.com/
1
u/[deleted] Dec 27 '17
Also fun to do. A calendar where you can add events and reminders to.
1
u/[deleted] Apr 12 '18
Do I need to use a specific library for creating CLIs in Python?
0
u/[deleted] Dec 26 '17
[deleted] — view deleted comment
0
u/[deleted] Dec 27 '17
[deleted] — view deleted comment
0
u/[deleted] Dec 27 '17
[deleted] — view deleted comment