Print Topic - Archive

XBLite Forum  /  Working Code Snippets  /  Command-line arguments
Posted by: caeman, June 29, 2008, 1:03am
A simple program that gets and displays what ever in the command line of the program.  This is my first step to other command-line programs.

--

'
' ####################
' #####  PROLOG  #####
' ####################
'
' A console program template
'
VERSION "0.0001"
CONSOLE
'
     IMPORT "xst"                    ' Standard library : required by most programs
     IMPORT "xsx"                    ' Extended standard library
     IMPORT "xio"                    ' Console input/ouput library

'     IMPORT "xst_s.lib"
' IMPORT "xsx_s.lib"
' IMPORT "xio_s.lib"

' IMPORT "gdi32"               ' gdi32.dll
'     IMPORT "user32"            ' user32.dll
'     IMPORT "kernel32"       ' kernel32.dll
'     IMPORT "shell32"          ' shell32.dll
'     IMPORT "msvcrt"            ' msvcrt.dll

'
DECLARE FUNCTION Entry ()
'
'
' ######################
' #####  Entry ()  #####
' ######################
'
FUNCTION Entry ()
     XstGetCommandLineArguments (@argc, @argv$[])
     PRINT "Number of command line arguments:"; argc
     
     FOR x = 0 TO argc-1
          PRINT argv$[x]
     NEXT x

END FUNCTION
END PROGRAM
Posted by: XBLiteAdmin, June 29, 2008, 11:47pm; Reply: 1
Quoted from caeman

     XstGetCommandLineArguments (@argc, @argv$[])
     PRINT "Number of command line arguments:"; argc
     
     FOR x = 0 TO argc-1
          PRINT argv$[x]
     NEXT x


When a function returns an array, it is usually better to make
sure it is not empty. So I would do this instead:

IF argv$[] THEN
  upp = UBOUND(argv$[])
  FOR i = 0 TO upp
    ? argv$[i]
  NEXT i
END IF

Posted by: caeman, June 30, 2008, 3:24am; Reply: 2
I figured in this case I could assume the array wasn't empty, since ARGV$ by its nature MUST have at least one entry.  But, yes, in most cases UBOUND should be used.
Posted by: Garquint, June 30, 2008, 11:48am; Reply: 3

You could do it like this too...i think:

XstGetCommandLineArguments (@argc, @argv$[])

FOR a = 0 TO UBOUND(argv$[])
  addr = &argv$[a]
  FOR b = addr TO addr+XLONGAT(addr-8 )
   chr$ = CHR$(UBYTEAT(b))
   IF (chr$ != "\0") THEN PRINT chr$;
   IF (chr$ == "\0") THEN PRINT
  NEXT b
NEXT a

but really not practical.



Garquint





Posted by: 17 (Guest), July 5, 2008, 10:38am; Reply: 4
What about copy/paste this demo?
Guy

PROGRAM "test"
VERSION "1.00"
CONSOLE
  IMPORT "xsx" ' for XstGetCommandLineArguments ()

DECLARE FUNCTION Entry () ' program entry point

FUNCTION Entry ()

'------------------------------------begin
  DIM argv$[]
  XstGetCommandLineArguments (@argc, @argv$[])
  upparg = UBOUND (argv$[])
  IF upparg < 1 THEN
    DIM parm$[]
  ELSE
    DIM parm$[upparg - 1]
    FOR iarg = 1 TO upparg
      parm$[iarg - 1] = TRIM$ (argv$[iarg])
    NEXT iarg
  END IF
'------------------------------------end
  IFZ parm$[] THEN
  ? "No line command arguments"
  ELSE
  ? "Line command arguments:"
    FOR i = 0 TO UBOUND (parm$[])
      ? parm$[i]
    NEXT i
  END IF
  a$ = INLINE$ ("Press Enter to quit >")
  QUIT (0)

END FUNCTION

END PROGRAM

-----------------batch file
@ECHO OFF
test.exe arg1 arg2 arg3
Print page generated: February 5, 2012, 2:37pm