What I mean by 'Ramble About'  
How to add Sublime Text 2 to your Gnome 3 applications.
Feb 05, 2012
9:22:54 pm

I'm a huge fan of Sublime Text 2. I'm also a pretty big fan of Gnome 3. The problem is that at the time of this post, they don't really like each other when you first get set up. So here's how it's done, son.

The basic trick is that you have to have a .desktop shortcut.

  1. If you haven't already run Sublime Text from it's installed location, do it now.
    Why? To unpack all the packages so we can use the icon.
  2. In gnome-terminal, run gnome-desktop-item-edit ~/.local/share/applications/Sublime\ Text\ 2.desktop.
  3. Fill in the Launcher Properties like so:


  4. Make sure the Command field is an absolute path to your sublime_text executable.
  5. To set the icon, click the "spring-board" icon on there, then navigate to: ~/.config/sublime-text-2/Packages/Default/Icon.png

When you press your super key and search for "Sublime", the launcher you just created should show up. Personally, the first thing I did was right click and "Add to Favorites".

Bonus points: Command line shortcut

  1. Open up your /home/you/bin folder in Nautilus.
  2. In a new window, navigate to your Sublime Text 2 installation.
  3. Hold Control+Alt+Shift and drag the sublime_text executable to the bin folder to create a symbolic link.
  4. Rename that link to "sub"

Now any time you're in terminal and you want to open the current folder in Sublime Text, you just type sub . I use this excessively.

to comment, login using [ google, twitter, facebook ]
PHP CLI sample (argument processing without getopts)
Feb 01, 2012
8:37:51 pm

I wanted a more versatile way to parse arguments in php, so I threw this one together. (This is a follow-up to my previous post.) Parsing arguments in php with getopts and such is fine, but if you want unix-like or even windows-like delimiting there's a bunch of funny logic that can go along with that. Re-writing an option parser from scratch every time you need one is well... dumb.

Admittedly, this option parser isn't as straight-forward as the use of boost in C++, but I think it sticks if you use it once or twice. (I wrote it, so how would I know?)

At the beginning of your php script

$app_name = array_shift($argv);
$arguments = array();
$arg_names = array(
    "-h" => "--help",
    "-t:" => "--test:"
);
$arg_count = count($argv);
for ($i=0; $i < $arg_count; $i++) { 
    $arg = $argv[$i];
    $is_arg = strpos($arg, '-') == 0;
    if ($is_arg) {
        foreach ($arg_names as $arg_name => $arg_long_name) {
            $expects_value = strpos($arg_name, ':') !== false;
            $eq_pos = strpos($arg, '=');
            $sets = $eq_pos !== false && $eq_pos != strlen($arg)-1;

            if ($sets) {
                $arg_value = substr($arg, $eq_pos+1);
                $arg = substr($arg, 0, $eq_pos);
            }

            if ($arg == str_replace(':', '', $arg_name) || $arg == str_replace(':', '', $arg_long_name)) {
                if (!$expects_value) {
                    $arguments[$arg_name] = true;
                } else {
                    
                    if ($sets) {
                        $arguments[$arg_name] = $arg_value;
                    }
                    else {
                        $i++;
                        $arguments[$arg_name] = $argv[$i];
                    }
                }
                break;
            }
        }
    }
}

function print_help($app_name) {
    echo "Usage: $app_name [-t|--test-arg] whattosetitto\n";
    echo "\t$app_name description and notes go here\n\n";

}

if (array_key_exists('-h', $arguments)) {
    print_help($app_name);
    exit;
}

$test_arg = $arguments['-t:'];

This method supports the following formats:

  • script_name -s "value"
  • script_name --full-name value
  • script_name --full-name=value
  • script_name -w
  • script_name --whatever
This allows the user to put things into the command line the same way they would for any standard unix/linux command.

Getting the values from the command line is done by just accessing an array using the switch's short-hand as the key. For Example:

//pull the value from an argument that has one;
// $# php ./args_test.php --argument="I just set this to anything I want."
$some_value = $arguments['-a:'];
echo $some_value; //outputs: "I just set this to anything I want."

//find out if someone turned on a non-settable argument
// $# php ./args_test.php -h
$is_switch_on = $arguments['-h'];
echo $is_switch_on === true; //outputs "1"
to comment, login using [ google, twitter, facebook ]
C++ CLI Template sample (argument processing without getopts)
Jan 20, 2012
6:49:04 pm

If you've ever had the need to make a C++ CLI application and just want to put something together quickly, you probably want to take in arguments from terminal. All right, but then you have to write all that out. I know it's trivial, but I hate writing that out every time. So here's a template to make your lives easier.

It uses boost libraries.

main.cpp

#include <string>
#include <boost/program_options.hpp>

using namespace std;
using namespace boost::program_options;

void print_help(options_description desc, string app_name) {
    cout << app_name << " [options]" << endl;
    cout << "\tList what the application does here." << endl;
    cout << "Options:" << endl;
    desc.print(cout);
    cout << endl << endl << "Example:" << endl;
    cout << "\t" << app_name << " sample command" << endl << endl;
}

int main (int argc, char * const argv[]) {
    //some docs first
    options_description desc;
    desc.add_options()
        ("help", "Shows this help message.")
        ("sample", value<string>(), "A sample thing.")
        ;

    //read the options
    variables_map vm_arg;
    store(parse_command_line(argc, argv, desc), vm_arg);
    notify(vm_arg);
    
    if (vm_arg.find("help") != vm_arg.end() || argc == 1) {
        print_help(desc, argv[0]);
    }

    //do your stuff here

    return 0;
}

Like I said, this guy uses boost so your Makefile will need to include the boost lib. Here's mine:

Makefile

CC = /usr/bin/gcc
LNK_OPTIONS = \
        -L/usr/lib \
        -L/usr/local/lib \
        -Llib \
        -lboost_program_options-mt 
        
DEBUG_OUTPUT = 1;

#ifeq ($(UNAME), Darwin)
MACOSX_DEPLOYMENT_TARGET_i386 = 10.6
MACOSX_DEPLOYMENT_TARGET_x86_64 = 10.6
SDKROOT_i386 = /Developer/SDKs/MacOSX10.6u.sdk
SDKROOT_x86_64 = /Developer/SDKs/MacOSX10.6u.sdk
CC = /usr/bin/g++
#endif

#
# INCLUDE directories
#
INCLUDE = -I. -I/usr/include -I/usr/local/include -Iinclude

#
# Build cli_template
#

cli_template: 
        rm -f ./main.o;
        make ./main.o 
    $(CC) $(INCLUDE) $(LNK_OPTIONS) \
        ./main.o \
        -o build/Debug/cli_template 

clean: 
        rm -f \
        ./main.o 

install:
        make cli_template;
        cp build/Debug/cli_template /usr/local/bin/cli_template

#
# Build the parts of cli_template
#

# Item # 1 -- main --
./main.o: main.cpp
    $(CC) $(CC_OPTIONS) main.cpp -c $(INCLUDE) -o ./main.o

If anyone has something they use that is cleaner, I'd love to see it. This just goes fast for me.

to comment, login using [ google, twitter, facebook ]
Finally started a blog.
Dec 30, 2011
10:55:00 pm

I know. There are tumblrs, blogspots, wordpress, etc. but I feel one is better served by creating something others already have -- if you have the chance. It gives you a chance to understand the problems that had to be solved by the creators of those other systems. Those lessons translate into other solutions for personal projects and jobs. Furthermore, when you have a problem with one of those third party systems in the future, you might just have a better understanding of what's causing it.

So just as a Jedi has to build a light saber to complete their training, I say you're not a web developer worth your salt until you've at least tried to build an MVC-type system from scratch and/or created your own blogging system rather than using a pre-built package.

Codey stuff will come soon.

to comment, login using [ google, twitter, facebook ]