help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: getopt


From: Judd Storrs
Subject: Re: getopt
Date: Sat, 10 Oct 2009 15:14:31 -0400

On Sat, Oct 10, 2009 at 1:50 PM, John W. Eaton <address@hidden> wrote:
> I would ask why you want to do this.

I agree that it's not very useful from within octave, but this would be useful for stand alone scripts (i.e. #!/usr/bin/octave).

I've tried going the getopt route before (and I actually implemented it somewhere) but in the end I decided I wanted to use long options to be more consistent other programs I was using. I work some on implementing getopts but it gets pretty hairy and I was having trouble with getting method handles to work. One idea I had was to write it as an .oct wrapper to the real functions. In the end I timed out and instead went with something like this (it's probably not very robust but it works and isn't too hairy):

## Command line defaults
opt.prefix = "" ;
opt.session = "" ;
opt.dim = [ 0 0 0 ] ;
opt.hamming = [ false false false ] ;
opt.dc = 32 ;
opt.kspace = false ;
opt.path = "" ;
opt.verbose = false ;
opt.skip = 0 ;
opt.b1cor = 0 ;
opt.rr = 0 ;

## Process command line options
args = argv() ;
i = 1 ;
while i <= length(args)
    option = args{i++} ;
    switch option
    case {"-prefix" "--prefix"}
        opt.prefix = args{i++} ;
    case {"-session" "--session"}
        opt.session = args{i++} ;
    case {"-b1"}
        opt.b1cor = str2num(args{i++}) ;
    case {"-dx"}
        opt.dim(1) = str2num(args{i++}) ;
    case {"-dy"}
        opt.dim(2) = str2num(args{i++}) ;
    case {"-dz"}
        opt.dim(3) = str2num(args{i++}) ;
    case {"-d"}
        opt.dim(:) = str2num(args{i++}) ;
    case {"-Hx"}
        opt.hamming(1) = true ;
    case {"-Hy"}
        opt.hamming(2) = true ;
    case {"-Hz"}
        opt.hamming(3) = true ;
    case {"-H"}
        opt.hamming(:) = true ;
    case {"-s", "--skip"}
        opt.skip = round(str2num(args{i++})) ;
    case {"-D", "--dc-points"}
        opt.dc = round(str2num(args{i++})) ;
    case {"-k" "--kspace"}
        opt.kspace = true ;
    case {"-r"}
        opt.rr = str2num(args{i++}) ;
    case {"-v" "--verbose"}
        opt.verbose = true ;
    case {"-q" "--silent"}
        opt.verbose = false ;
    otherwise
        opt.path = option ;
        break ;
    endswitch
endwhile



--judd


reply via email to

[Prev in Thread] Current Thread [Next in Thread]