Why D Needs Attributes (Was: Command-line arguments)

Nick Sabalausky a at a.a
Thu Jul 3 23:57:36 PDT 2008


"Nick Sabalausky" <a at a.a> wrote in message 
news:g4k93l$1mh$1 at digitalmars.com...
> "Jarrett Billingsley" <kb3ctd2 at yahoo.com> wrote in message 
> news:g4ju5s$2dh0$1 at digitalmars.com...
>> "Matt" <no-one at none.nowhere.com> wrote in message
>> news:g4jqum$269v$1 at digitalmars.com...
>>> Is there an established library in D for handling command-line 
>>> arguments?
>>>
>>> In particular:
>>> - handling differences between Windows and UNIX shells (i.e. wildcard
>>> expansion)
>>> - handling equivalent options such as "-n 10" === --count=10
>>> - handling combination of multiple flags, e.g. "-lcf" == "-l -f -c"
>>>
>>> Thanks
>>>
>>> Matt
>>
>> I don't believe there is an argument parser in either Phobos 1 or Phobos 
>> 2.
>> However, there is one in Tango.
>>
>
> Command-line parsing is one of the big reasons I really wish D had C#/Java 
> style attributes. I came across this one particular C# command-line parser 
> (written by a Peter Hallam) a few years ago and absolutely fell in love 
> with it, and by extention, attributes. (I'd link to it, but it was on 
> GotDotNet, which MS replaced with something else and didn't move this 
> particular program over. So I'm attaching the lastest version I have (only 
> about 11k), even though there are newer versions...somewhere.)
>
> Just check out how awesomely simple it makes this sample case:
>
> BEGIN CODE
> class WCArguments
> {
>    public bool lines;
>    public bool words;
>    public bool chars;
> 
> [Utilities.DefaultCommandLineArgument(Utilities.CommandLineArgumentType.MultipleUnique)]
>    public string[] files;
>
> 
> [Utilities.CommandLineArgument(Utilities.CommandLineArgumentType.AtMostOnce, 
> ShortName = "num")]
>    public int number;
>    public string[] junk;
> }
>
> class WC
> {
>    static void Main(string[] args)
>    {
>        WCArguments parsedArgs = new WCArguments();
>        if (!Utilities.Utility.ParseCommandLineArguments(args, parsedArgs))
>        {
>            // error encountered in arguments. Display usage message
> 
> System.Console.Write(Utilities.Utility.CommandLineArgumentsUsage(typeof(WCArguments)));
>        }
>        else
>        {
>            // insert application code here
>        }
>    }
> }
> END CODE
>
> The WCArguments class is itself the very definition of the program's 
> command-line arguments. And attributes can be used to specify things like 
> "DefaultCommandLineArgument", "MultipleUnique", "AtMostOnce", "ShortName", 
> "Required", etc. Later versions include automatically-generated help 
> screens. And the command-line syntax is the same 
> ultra-clean-consistent-and-unambiguous commandline syntax that MS's 
> command-line .NET tools use.
>
> D2 could probably come close to this with its compile-time reflection, but 
> I don't think there'd be a comparably simple way to specify the things 
> that this uses attributes for.
>

I just took a look at Phobos2's getopt. It's impressively code to this in 
functionality. But I still think this is much cleaner and more DRY 
("drier"?)





More information about the Digitalmars-d mailing list