News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Command Line Arg Handling - Switch Options

Started by fearless, January 31, 2011, 07:08:37 PM

Previous topic - Next topic

fearless

Hi,

I have been making a few console type program lately and have a few that requires a few switches and values some optional others required. I used Vortex's CmdLnParser from http://www.masm32.com/board/index.php?topic=5319.0 to help with this. This works as advertised and is fine. I have a processcmdline proc that uses a lot of instring comparisons and checks the no of actual params to build a sort of logical structure to follow, its a bit messy, and if i have to check for optional switches and if filenames exist or if a value is just numeric etc then it adds a huge amount to the code. Also its not flexible as the parameters have to be in a specific order.

I've also considered recently to rewrite my existing procedures and loop through each cmd line param looking for a specific switch at any position and fetch the optional value (if applicable) and do some validation.

example of what im talking about might help.

Program usage:
MyProg /?
MyProg /U:infilename [/O:outfilename] [/S]
MyProg /C:infilename [/O:outfilename] [/S]
MyProg /F:frameno|* [/X:XCo-ord] [/Y:YCo-ord]
MyProg /I:infilename


Program is for helping mod a graphics file format for baldurs gate game (*.bam file) which can be compressed or an uncompressed version.

/? shows help on console.
/I gives information about the bam file, including no of frames images, offsets etc etc - some technical info.
/U Uncompress file (if compressed .bam) to outfilename.
/C Compress file (if uncompressed .bam) to outfilename.
/S safety switch, used with /U or /C to designate outfilename to have a different extension, so as to preserve the original file.

If no outfilename specified then infilename can be wildcards (* ?), and outfilename is automatically set to infilename. If optional /S (safe mode) switch is used outfilename gets a .bamc or .bamu extension to represent compressed or uncompressed and leaves source file untouched. Skips if file is compressed/uncompressed already depending on switch supplied (see /U or /C)

/F:frameno|* is frame number to display information for. Numeric only. or if * is supplied all frames within .bam file are listed.
/F:frameno|* /X:XCo-ord is frame number to set the X co-ordinate for.
/F:frameno|* /X:XCo-ord /Y:YCo-ord is frame number to set the X co-ordinate for and Y co-ordinate for.
/F:frameno|* /Y:XCo-ord is frame number to set the Y co-ordinate for.

some valid data examples:

/F:0 /X:10 /Y:20
/F:3 /X:13 /Y:25
/F:* /Y:15
/F:25
/F:25 /X:3
/U:*.bam /S
/U:test.bam /O:tester.bam
/C:tester.bam tester.bamc
/I

As you can see there are lots of options and permutations, and whilst what i have works, im wondering what others do or use? is there a more elegant solution for processing all the switches and values? Should i make a data table or structure that has 'rules' for what variable the parameter should have. Like a column that has 0 for no value (switch on its own for example), 1 for numeric, 2 for alphabetic, 3 for alpha-numeric, 4 for valid pathfilename etc etc, and other columns for minlength/maxlength etc.

Thanks for talking the time to read through this and hopefully someone has a better/more elegant solution than what i currently have coded.

Best regards


ƒearless

qWord

what about using regular expressions for parameter validation?
FPU in a trice: SmplMath
It's that simple!

jj2007

Or a jump table? You would probably need a tailored CL$() to handle e.g. /A:"The file with spaces".

Quoteinclude \masm32\MasmBasic\MasmBasic.inc
Dispatch   dd swa, swb, swc, swx, swe, swf, swg, swh, swx, swx, swk, swx, swx, swn, swx, swx, swx, swx, swx, swx, swx, swx, swx, swx, swx, swz
   
Init
   [/color]xor ebx, ebx
   .Repeat
      inc ebx
      mov esi, CL$(ebx)   ; get commandline arg(ebx)
      .Break .if !esi
      lodsb
      .Break .if al!="/"
      lodsb
      .Break .if al<"A"
      or al, 32
      .Break .if al>"z"
      lea edx, [esi+1]
      and al, 31
      movzx eax, al
;      int 3
      call Dispatch[4*eax-4]
   .Until 0
   .if esi
      dec esi
      print esi, ": final arg"
   .else
      print "NO final arg"
   .endif
   Inkey Str$("\n\n%i options found", ebx-1)
   
Exit
swx:
  print edx, 9, ": no such option", 13, 10
  retn

swa:
  print edx, 9, ": Option A", 13, 10
  retn
...
end start

fearless

Thanks for the replies, I'll have a look at both options. With the regular expression option i only know the very basic idea about it and don't know how to implement it in asm. I presume there might be a function or library somewhere that allows me to use regular expression matching, so I'll have a look for that as well. Thanks.
ƒearless

hutch--

fearless,

Have a look at the masm32 library function "wtok proc pText:DWORD,pArray:DWORD" in wtok.asm.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

fearless

ƒearless