Ayao "Alqualos" Kuroyuki (ayao) wrote,
Ayao "Alqualos" Kuroyuki

Unix philosophy: reuse! Popt example

One of the concepts of the Unix philosophy is code reusing. Never write anything that is already written. Never invent the wheel. If you are not happy with any of the existing wheels, try to modify one of those instead of writing your own.

So I was writing class QuaArg for parsing command-line arguments, knowing that there are already such things as getopt and getopt_long. I was doing that because I needed more portable thing - I needed it to work on Windows too, for example. And I am not even sure whether getopt_long will work on HP-UX, for example.

But then I realized that I was still doing something wrong. The very idea of reimplementing such common thing as command-line arguments parsing by myself started to seem more and more ridiculous to me. So I stopped and thought a bit. Okay, so I am in need of portable command-line argument parsing library. Is it hard to implement? Not. Plain standard C is more than enough. So, am I the only one who needs it? Probably not. Therefore, there must be something like portable getopt_long implementation! There is no way it would not exist!

So I started to look for it and quickly found GnuWin32 project. There I looked for getopt_long, did not found it, but found popt instead. It is a library for command-line option parsing in C. A few external dependencies, but they are all portable C libraries too. Everything is available for Windows in that project. And it is of course available on any Unix, too. Easy to use, has all the features I need. I tried to use it, and it worked fine. I tried to compile then on Windows - worked fine, I only had to put DLLs in PATH, and properly set up paths to header files and libraries.

Now I already implemented half of my application that needed to use command-line arguments. If not for Unix philosophy, I would probably still be coding that QuaArg class, finding and fixing a lot of bugs and watching my code grow far beyond two simple functions.
Tags: devel, it, unix
  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded