The Kaptain on … stuff

04 Oct, 2009

Groovy CliBuilder with multiple arguments

Posted by: TheKaptain In: Development

I’ve been writing a lot of Groovy scripts lately and have developed quite a fondness for the CliBuilder along the way.
There’s lots of great examples on the internet, but I could only find one place that demonstrated how to easily consume an unknown number of parameters as a List and that was a copy of the CliBuilderTest I found on Koders. There’s also some explanation for the different behavior inherent in the Apache Commons Cli libraries V1.0 and V1.1 that CliBuilder encapsulates(there is a 1.2 version, but from the documentation it seems CliBuilder may not be fully compatible with it).

Here’s a GroovyTestCase that exercises CliBuilder with multiple arguments.

[groovy language=”true” stripbrs=”true”]
package org.kar
import org.apache.commons.cli.Option
/**
* Demonstrates usage of the CliBuilder with multiple arguments to create a List.
*/
class CliBuilderDemoTest
extends GroovyTestCase {
/**
* You can specify multiple arguments one at a time.
*/
void testMultiOption() {
CliBuilder cli = new CliBuilder()
cli.with {
a longOpt: ‘arguments’, args: 2, required: true, ‘Two arguments’
}
def args = [‘-a’, ‘arg1’, ‘-a’, ‘arg2’]
def options = cli.parse(args)

assert (options)
assertEquals(‘First arg is available with the -a option. ‘, ‘arg1’, options.a)
assertEquals(‘Should be two args, in order, available with the addition of an "s" to the option.’,
[‘arg1’, ‘arg2’], options.as)
}

/**
* You can specify multiple arguments together in a block, with a defined separator(in this case a comma).
*/
void testMultiOptionWithSeparator() {
CliBuilder cli = new CliBuilder()
cli.with {
a longOpt: ‘arguments’, args: 2, required: true, valueSeparator: ‘,’ as char,
‘Two arguments, separated by a comma’
}
def args = [‘-a’, ‘arg1,arg2’]
def options = cli.parse(args)

assert (options)
assertEquals(‘First arg is available with the -a option. ‘, ‘arg1’, options.a)
assertEquals(‘Should be two args, in order.’, [‘arg1’, ‘arg2’], options.as)
}

/**
* You can also have any number of arguments by specifying UNLIMITED_VALUES.
*/
void testUnlimitedArgs() {
CliBuilder cli = new CliBuilder()
cli.with {
a longOpt: ‘arguments’, args: Option.UNLIMITED_VALUES, required: true, valueSeparator: ‘,’ as char,
‘Two arguments, separated by a comma’
}
def args = [‘-a’, ‘arg1,arg2,arg3’]
def options = cli.parse(args)

assert (options)
assertEquals(‘First arg is available with the -a option. ‘, ‘arg1’, options.a)
assertEquals(‘Should be a list of args, in order.’, [‘arg1’, ‘arg2’, ‘arg3’], options.as)

def args2 = [‘-a’, ‘argOnly’]
def options2 = cli.parse(args2)

assert (options)
assertEquals(‘First arg is available with the -a option.’, ‘argOnly’, options2.a)
assertEquals(‘Should be a list of args, with a single entry.’, [‘argOnly’], options2.as)

def args3 = []
//this will automagically print the usage string and any validation errors to System.out
def options3 = cli.parse(args3)
assertNull(options3)
}
}

[/groovy]

Anyhow, hope that the next guy finds this info useful – it certainly has made my recent work writing Groovy scripts much easier!

27 Sep, 2009

Twitter Weekly Updates for 2009-09-28

Posted by: TheKaptain In: TweetTweet

Powered by Twitter Tools

20 Sep, 2009

Twitter Weekly Updates for 2009-09-21

Posted by: TheKaptain In: TweetTweet

  • If only it were every morning ๐Ÿ™‚ RT @jstrachan: I love the smell of a brand new MacBook Pro in the morning… #
  • @mosabua @ajordens Burndown charts are where it's at man. Without Burnination there can be no progress ๐Ÿ™‚ #
  • Spread the word to vote for #maven in #ubuntu to be updated to the latest version as packaged in @debian. http://bit.ly/1ahEos Please RT. #
  • @tylerblack Excellent choice! in reply to tylerblack #
  • @tylerblack Of a 100 balls crazily flying in the air, no one is capable of keeping them all afloat. And knowing you, it won't be repeated. in reply to tylerblack #
  • RT @scottdavis99:"Getting Started with Grails" (2nd ed) is at the typesetter.It covers Grails 1.2. Should be available in a couple of weeks. #
  • @dillyh how's the noggin man? Paying the price for the good tunes? #
  • Arrrrgh. Love Pirate day so much I subverted me blog maties! #

Powered by Twitter Tools

13 Sep, 2009

Twitter Weekly Updates for 2009-09-14

Posted by: TheKaptain In: TweetTweet

Powered by Twitter Tools

09 Sep, 2009

A Techno-Geek’s Guide to Encyclopedic Knowledge

Posted by: TheKaptain In: Development

Do you make keeping up with technology a priority? Have you found a way to prevent drowning in the vast sea of content AND still keep abreast of everything you want to? Me neither, but here’s where I go to learn new things, find solutions and keep up with the general state of the union regarding software I use; not to mention keeping track of what new tools are available to speed development.ร‚ย  This is far from an exhaustive list but I wanted to concentrate on primarily free sources of information and tooling. Yes ladies and gentlemen pretty much everything mentioned on this page requires at most an internet connection and a web browser to use.

DISCLAIMER: yes, this is heavily geared towards(but not exclusive to) Groovy-esque sources. What can I say – it’s where I spend most of my time.


Google… in General

If you want to find something – well let’s just say any service that is so pervasive it becomes a verb in the language might be useful.ร‚ย  Learning how to harness the power of the Google architecture can greatly enhance your learning experience. And I can’t see myself Bing’ing anytime soon.

  1. Google Search: Knowing and using the Google search syntax properly can be a very powerful tool for finding specific tech-geekie knowledge.
  2. Google Reader: Listening to the right content can save you a lot of time when it comes to getting the most out of your code. Even if you don’t read it everyday, combined with Gears and searchability this provides another tool in the fight against techno-ignorance that doesn’t rely on an internet connection.
  3. Google Mail: I combine mailing list subscriptions with filters to automatically organize content. Incoming messages from lists are archived and labeled accordingly by filter rules. With Gears installed I’ve got an off-line searchable database of grassroots questions and answers, similar to what an on-line service like MarkMail provides. It’s hard to be the ongoing discussion of community members sharing their best tips, tricks and gotchas.
  4. Google code: A great place to find open source projects that can assist you at work and provide valuableร‚ย  learning materials. And yes, the link targets Groovy :).


Aggregators

Some kind people do most of the work for you – taking themed content and putting it all together in one place.

  1. DZone: One of my favorite sources ofร‚ย  techie style news.
  2. GroovyBlogs: A great example of ‘eating your own dog food’, this is a Grails powered site courtesy of one of the authors of Grails in Action. Most of the Groovy related blog articles I read come from this source.
  3. Grails Tutorials: A nice collection of tutorial articles related to Grails and Groovy.
  4. GR8Forums: Brand new, this site has the potential to be a great discussion spot.


Community Sharing

There are certain places that encourage community and knowledge sharing more than others.

  1. Groovy Console in The Cloud: This is an amazing example of an application exposing a Groovy console on the Google AppEngine, complete with community support for running, saving and commenting example code.ร‚ย  Since the code is freely available, there’s also already a Spock console here in the cloud forked/cloned from it.
  2. Github and Gists: More places to share code in project and/or snippet form.
  3. StackOverflow: A community effort to put together almost 300,000(and rising) questions and answers related to programming. If you have a question to ask, head over here – chances are someone else has already given an answer. It really is impressive how well this still evolving site functions.
  4. Podcasts: JavaPosse and the Grails podcast are on top of my list and usually get listened to during my commute to work.There are hundreds of tech related podcasts out there; pick one or two and give them a listen,ร‚ย  you won’t regret it.
  5. mrhaki : OK, this guy gets special mention just for busting out code examples for pretty much every Groovy feature you can possibly imagine. Seriously the guy posts 20+ times a month on average.


Home Pages

These aren’t necessarily up to date at all times, but are great places to start looking for generalized knowledge and additional resource material.

  1. Groovy: Pay particular attention to the ‘Cookbook Examples’ and the ‘Modules’ available to quickly add functionality.
  2. Grails: A particularly rich and well organized site – with the latest update it’s also very pretty.
  3. Griffon: The builder documentation linked from here, combined with the example scripts included with SwingPad, get you up to speed quickly on all of the buildery goodness available in Griffon.


Useful and Inspiring

These are resources I use to step out of the box and to remind myself that it’s not only in elegant code that geekiness lies.

  1. Lifehacker: I get into trouble a lot for this one; in my house whenever someone asks me “How do I do {fill in the blank}?” I invariably respond with “Well what does Lifehacker say?” My personal favorite is the bundle wrapping method. With a nice mix of techie and common sense goodness, I love to follow this site.
  2. Make magazine: In need of a DIY project to fill up the weekend? From the Minty Boost for a $20 portable USB charger to robots this place takes geeking out entirely to the next level.
  3. Jonathon Coulton: With eye-popping geekiness, Jonathon brings us such wonderful classics as “Code Monkey” and “Re Your Brains” – and under a Creatives Commons license as well!


Twitter

Use with caution. More than any other source of data, this one can end up being seriously distracting. I’m careful about who I listen to, while simultaneously trying to remind myself that nobody cares what I had for breakfast or how much I enjoyed it. Have you tried Twitter yet? Not as easy as it sounds buster, believe you me. Just wait and see. If you can keep yourself from jumping every time a new Tweet lands at your desktop, this can be the freshest, fastest way to satisfy your tech cravings.


Books

OK, so these aren’t free, but if you want to jumpstart your knowledge on a particular piece of technology, software or framework, picking up a recent book on the subject can truly help. I try to add a new book to the library every month, but the hard part is really finding the time to read and apply the new knowledge.



I don’t expect that these suggestions are for everyone, but hopefully a few of you out there in the interwebs will find some of them helpful. And feel free to share what works for you if I’ve missed anything.






Reblog this post [with Zemanta]



06 Sep, 2009

Twitter Weekly Updates for 2009-09-07

Posted by: TheKaptain In: TweetTweet

  • 50% on any Manning MEAP ebook today with coupon pop0901! get your copy of Groovy/Grails/Griffon in Action! ๐Ÿ˜‰ (via @aalmiray) #
  • Just watched @tylerblack give an AWESOME software demo. Smooth man, very very smooth! Great work from the entire team to pull it off! #
  • @tylerblack Nice! Never even noticed your snaggletooth. in reply to tylerblack #
  • RT @rhyolight: Why people fear the GridBagLayout: http://pastie.org/603439 Yukkers indeed! #
  • RT @CKNW: Province restores funding 2 arts groups Just 1 day after defending cuts to arts groups across BC, the provin.. http://bit.ly/Nb6DF #
  • RT @Rod_Phillips: #yyj 1st 50 replies or RTs to #yyjtreebrewing, $6 6 pack of either Tree Hophead or Tree Cuthroat Pale Ale. #
  • @Rod_Phillips Saanich please – and once again tyvm for running these promotions! #
  • Mmmm. I want one! http://bit.ly/4yoIgR #
  • @Rod_Phillips sorry forgot the beer choice. Cutthroat please, at Saanich location in reply to Rod_Phillips #
  • RT @aalmiray: Griffon, a synonym for "Polyglot Programming Desktop/RIA Development Framework" #
  • @glaforge I have no idea what "power assert" is but it sure does sound sexy in reply to glaforge #
  • @glaforge LOL, I try. But you're a busy, busy guy and it's hard to keep up with all of it ๐Ÿ˜‰ in reply to glaforge #
  • Good offsite today. Nice to be out with the team, sip a latte, and still get some work done ๐Ÿ™‚ #
  • RT @mittie: The GParallelizer team has grown in size – @alextkachman joined us. (via @vaclav_pech) And congrats @mittie too! #
  • @ajordens @abstratt I second that bad taste from Borland ๐Ÿ™ in reply to ajordens #
  • RT @Rod_Phillips: #yyj 1st 50 replies or RT's to #yyjvinamaipo gets either Vina Maipo Carmenere or Sauv Blanc for $5 (reg$12/btl) #
  • @Rod_Phillips Thanks very much once again Rod! A Carmenere in Saanich this evenings sounds fantastic. in reply to Rod_Phillips #
  • RT @sbouchard: Phillips Blue Buck Ale, litre for litre has to be one of my most favourite beers. #beer #friday (via @Rod_Phillips) #

Powered by Twitter Tools