Friday, May 02, 2008

Parse command line arguments with Args4J

Args4J is a useful library to parse command line arguments, instead of doing it by hand. I'm very pleased with its simplicity, something I didn't find in others libraries.

Recently I've been working on some small console applications in Java. Like most of these applications, it's often needed to pass them some arguments at the console. For a while I parsed those arguments by hand. However, as soon as I had to do it for a large enough amount of arguments I started searching for a library to simplify things.
Jakarta Commons has all sort of useful libraries and Commons CLI is one of them. And for a while I used Commons CLI. But although the code became more organized I felt I had to write as much lines of code as before!
So after another search I found Args4J. This library makes use of annotations and has a much smaller API to use. It's not as flexible as Commons CLI, which allows you to parse different styles of arguments, etc. But I don't need that, I just wanted something to simplify the task. And args4j does simplify. Here's an example of how it works:

Imagine a sample application that tests some external system by sending a request to a specified machine. This application would need the following arguments: -h (the host of the external system) and -p (the port of the external system). This would translate to the following command in the console: "tester -h somemachine.com -p 9000".
The main class should look something like this:


public class Tester {

@Option(name="-h", usage="host of the external system", required=true)
private String host;

@Option(name="-p", usage="port of the external system")
private int port = 80;

public void setHost(String h){ this.host = h; }
public void setPort(int p){ this.port = p; }

public static void main(String[] args) {

Tester t = new Tester();
t.run(args);
}


public void run(String[] args) {

CmdLineParser parser = new CmdLineParser(this);
try {
parser.parseArgument(args);

//do whatever ...
System.out.println("Testing host " + host + " on port " + port);

} catch (CmdLineException e) {

System.err.println(e.getMessage());
System.err.println("\nTester [options...] arguments...");
parser.printUsage(System.err);
}
}
}

As you can see from the sample above, the clever usage of annotations in this case allows the definition and description of the parameters in a very simple way.
  • name is the argument name that should be typed in the console
  • usage is the description that is shown in case you don't type a valid set of arguments
  • required tells arg4j that the specified argument is mandatory (see documentation for more options).
Also, the API is quite small and does all the validation for you. In this case, the "host" parameter is required, so args4j will throw an exception if it's not supplied. And port parameter is automatically converted to "int". An error is also thrown if it can't convert it.
If validation succeeds, the values supplied will be bound to the corresponding fields in the class. Also notice that port is not required and has a value defined. This is basically a default value that will be replaced by the supplied argument, if it receives one.
So, with arg4j, all you have to do is annotate the fields, put the line of code that parses the arguments inside a try-catch block and safely use the fields. That's pretty cool.

Wednesday, March 26, 2008

Howto write an application for social networks

I've been seeing more and more applications for social networks (like Hi5). What I didn't realise was that it existed a standard API for developing those applications. OpenSocial is a reference API by Google that is being used by some of the major social networks (LinkedIn, Hi5, MySpace). It makes sense as it allows a developer to create an application or gadget that works in several social networks, or even allows communication between those.
And this explains the amount of new applications that are emerging every day. Still, it amazes me how a consensus was reached to allow this to happen. I thought only W3C had the power to do that. On the other hand, Google is becoming more "standard" than anything else...
On the technical side, the OpenSocial site seems quite good, with lots of documentation and sample applications. I didn't have the time to read the API itself, but being so well documented it's a great thing. It makes me think it must be easy to implement this kind of applications. It makes me wanna implement something :)
This also makes me wonder how many interesting Google projects are there that I still never heard of...

Friday, March 07, 2008

Exhibit 2.0 - Javascript web framework

I just discovered Exhibit and I'm amazed by its simplicity. Exhibit is a web framework written in Javascript that you can use to display data. It's not a general purpose framework. It's focused on displaying data in several formats, like interactive tables, maps, graphs, etc.
What I like about Exhibit is how simple it is to use. Everything is done in a simple HTML file! You heard me right, just HTML. The framework is done in Javascript, and it does all the data manipulation. You just have to add some special attributes to the HTML tags in order to specify how data is going to be shown. That's the presentation part of the framework. The data itself has to be in JSON format. Just add a link tag pointing at the js file with your data structure and you're all set. This is actually very flexible. In a simple case you can point to a static js file. But you can also point to an URL that dynamically generates your JSON data.
Exhibit then gives you a few presentation choices. One of them is trough Google Maps. This way you can create mash ups quite easily. Interactive tables that allow filtering and sorting also look nice.
Exhibit is part of SIMILE project, a joint effort conducted by the MIT Libraries and MIT CSAIL. So simple, but it can be quite useful...and pretty cool.

Thursday, March 06, 2008

Pyhton developers hired by Sun

This is interesting news:


Python’s future looks bright by ZDNet's Ed Burnette -- It always warms my heart to see good programmers get the recognition they deserve. This week, Sun announced they were hiring Ted Leung (long-time Python developer), and Frank Wierzbicki (lead implementer of the Jython project). They’ll be working full-time on Jython and in particular paying attention to developer tools. Ted and Frank join Charles Nutter, Thomas [...]


Although Jython is not new, this shows the current trend in Sun: to support as many languages as possible in the VM. It's nice to see that not all eggs are being put in the same basket (Ruby) and Python is also getting some attention. I specially like that because I prefer Python over Ruby. This also reminds me of putting Jython in my ToDo list of technologies to check out :)

Thursday, February 28, 2008

PDFEdit - Editing PDF in Linux

Did you ever had the need to alter a PDF file ? For me, it doesn't happen very often, but when it does I can only think of Acrobat Writer. I know I have also searched the Internet for some free tool, but it's not easy to find one. I've searched for "editing pdfs in Linux" before and come up with nothing but some technique involving saving each page to PostScript, then editing on an image editor, etc...
So it was with a bit of surprise that I found PDFEdit. It does its job well and has lots of features. At first glance seems like an application that would be very well known. But it's not the case, and it definitely deserves more spotlight. Check the screenshot below (taken from PDFEdit website):


You can add text, highlight sentences, add pictures, etc. Almost like any other text processing tool. Very cool.

Wednesday, February 27, 2008

Htop - Manage Your System Processes in Seconds

Almost everyday we use top to check which processes are using more cpu/memory. It's such a basic tool that I have never spend time searching for something better. Nevertheless, I never found top to be very intuitive or fast. The article Htop - Manage Your System Processes in Seconds
shows an excellent alternative. It starts much faster and it's much easier to use than top.
Just to show you an example: if you want to change the order in which processes are displayed, just press F6 and select with your up/down keys the criteria (cpu, memory, etc). Press Enter and voilá. Now try the same on top! Yes, you can do it with top almost as fast, but only if you can remember the weird shortcuts. If you use top ocasionally, then using htop is much simpler and intuitive.
Another example is killing a process: select the process with your up/down keys and press F9.
Just a note for the article author: very good blog, focused and to the point.

Tuesday, January 15, 2008

Thumb navigation and Pointui

It's funny to see the evolution in user interfaces for mobile devices. The evolution from static simples screens to touch screens was a huge step. Allowing a user to write with a plastic pen on a screen was incredible. That evolution allowed the user to do more with his mobile device, at the cost of complexity. There's no doubt that the introduction of the touch-screen and the pen increased the complexity of the user interface. And that's because it allowed the user to do more. It almost mimics the desktop computer nowdays. So, that's a good thing...or not ? In that time it was a great thing....now, we want to be able to the same things but with a simplified interface. That's the most recent evolution: thumb navigation aka "getting rid of the pen and allowing us to use our fingers" :)
I think what drove this evolution was the merge between the phone and the pda. We don't mind using a pen with a PDA. We are used to it. But we are not used to need a pen to use a phone. That's just to much work. I need to access my phone fast and hassle-free. Sometimes I only have one free hand to answer the phone or make a call, it must be able to do it.
Besides all the eye-candy, I think that was the great evolution that iPhone popularized. For me, I don't own an iPhone, but I own a windows mobile 5 device. And this platform hasn't seen any user interface improvements in years. It's almost the same I used several years ago, when I first saw a Pocket PC. So, I have to resort to third-party applications to make my user experience more pleasant.
Pointui is a free application that substitutes wm5 desktop with a thumb-navigation interface with a lot of eye candy, resembling iPhone in many aspects (like scrolling a list). It's not completely finished yet (it's a beta) but for me it's better than using wm5 desktop. You can see a video of what it feels like.

The veredict is: I haven't found many bugs, but there are some things lacking to completely substitute wm 5 interface in day-to-day tasks. Besides that, it's great and fun to use. Did I mention it's free ? :) Go ahead and try it.

Friday, January 11, 2008

Gentoo and Ubuntu 7.10

A few days ago I installed Ubuntu 7.10. I needed something quickly and went for Ubuntu. As you might know I used Gentoo for several years, so this is quite a change...although I haven't decided if I'll keep Ubuntu or install something else.
Before this, I was curious about Sabayon and I'll probably try it in a near future. For those who don't know, Sabayon is a flavor of Gentoo. Sabayon has a nice install with many packages pre-built, so that you don't have to compile everything.
From this, you might think I got tired of compiling software. Maybe a little, but that was never the reason why I liked Gentoo. I liked Gentoo because of portage, the huge repository, good community-based documentation for almost anything, and even having to hack one thing or another... what I don't like is having to wait so much time to install a package.
In comparison to Ubuntu, obviously I can install things much faster but I lose the flexibility that portage gave me. Not the "flexibility" of compiling the software...the flexibility of choosing which version of the software I want (not just the last) and which features I want compiled in the software. In the past I had issues with this in Ubuntu: a package was available in repository, but compiled without a feature I needed. Another thing (related to this) I dislike in Ubuntu is the huge dependency tree that I have to pull when installing some packages. In part that's because in Gentoo I always had several features disabled (like Kde or Gnome integrations if I don't use them) which saved me from installing a bunch of libraries that I didn't need.
Well, for now I'll be using Ubuntu and I already found several good/bad points.

  • Something I liked was the hardware detection. It detected everything pretty well, including my webcam, printer and digital camera.
  • Boot time is fast, even gnome starts fast (but I prefer Xfce, which is always faster)
  • Good collection of GUIs for system configuration.
What I didn't like was some surprising bugs:
  • login window sometimes is just a white screen
  • my wallpaper disappears sometimes, leaving me with a pale blue background
  • flash plugin for firefox is broken (fixed it manually)
  • some strange effect in window titles that disappear, under Gnome
  • Compiz Fusion is not available under XFCE (I'll try to fix this by hand when I have the time)