home *** CD-ROM | disk | FTP | other *** search
/ Point Programming 1 / PPROG1.ISO / basic / bmag / cmdparse.bas < prev    next >
Encoding:
BASIC Source File  |  1994-04-26  |  2.6 KB  |  75 lines

  1. '─ Area: F-PowerBASIC ─────────────────────────────────────────────────────────
  2. '  Msg#: 8                                            Date: 18 Apr 94  01:33:00
  3. '  From: Arnie Sossner                                Read: Yes    Replied: No 
  4. '    To: All                                          Mark:                     
  5. '  Subj: COMMAND$ parsing
  6. '──────────────────────────────────────────────────────────────────────────────
  7. 'I've seen lots of requests to parse COMMAND$ in the QuikBASIC FIDOnet
  8. 'echo.  It's amazingly easy in PowerBASIC version 3 using the EXTRACT$
  9. 'and REMOVE$ commands.  I've coded up the following demo for your
  10. 'edification.
  11.  
  12. ' MYPARSE.BAS Puts each command line operand into an array. Reports number of
  13. '             operands.
  14. ' 4/17/94 - First release - by Arnie Sossner - Prodigy: VRPT68A
  15.  
  16. ' For sake of demonstations, start this program with various operands.
  17. ' For example:
  18. '   Demo 1: MYPARSE
  19. '   Demo 2: MYPARSE a B c D
  20. '   Demo 3: MYPARSE /Op1 /Op2
  21. '   Demo 4  MYPARSE /?
  22.  
  23. ' 4/17/94 - First release AP Sossner - Prodigy: VRPT68A
  24.  
  25. 'variables
  26. DIM Operand$(1 TO 20)              'space for twenty operands ought to suffice
  27. Sep$=" "                           'operand separator character
  28.  
  29. 'main program
  30.   CLS
  31.   IF LEN(COMMAND$)<>0 THEN
  32.      PRINT "The command line to be parsed: "+COMMAND$
  33.      CALL ParseCommandLine(Operand$(),OperandCount,Sep$)
  34.   ELSE
  35.      PRINT "No command line operands."
  36.      END IF
  37.   'demo of efficacy
  38.   PRINT "The number of command line operands: "+STR$(OperandCount)
  39.   FOR i = 1 to OperandCount
  40.     PRINT "Operand"+STR$(i)+": "+Operand$(i)
  41.     NEXT i
  42.   END
  43.  
  44.  
  45. 'subroutines
  46. SUB ParseCommandLine(Operand$(),OperandCount,Sep$)
  47.   'Parse command line for operands. Array starts at 1, no 0; hence Operand$
  48.   'index is incremented by 1.
  49.   Cmd$ = LTRIM$(RTRIM$(UCASE$(COMMAND$)))    'your appl might not want UCASE$
  50.   OperandCount = 0
  51.   DO
  52.     Operand$(OperandCount+1) = EXTRACT$(Cmd$,Sep$) 'extract string to 1st Sep$
  53.     Cmd$ = REMOVE$(Cmd$,Operand$(OperandCount+1))  'remove Op(i) from Cmd$
  54.     Cmd$ = LTRIM$(Cmd$)  'Change this line if Sep$ is not a space character
  55.     OperandCount = OperandCount + 1                'get next Operand$
  56.     LOOP UNTIL LEN(Cmd$) = 0
  57.   END SUB
  58.  
  59.   'you might want a SELECT to decode the operands either in Main or a SUB
  60.   'SUB ProcessOperands ...
  61.   'DO
  62.   '  FOR i = 1 TO OperandCount
  63.   '    SELECT CASE Operand$(i)
  64.   '      CASE "/?"
  65.   '        ShowHelp
  66.   '        SYSTEM
  67.   '      CASE "A"
  68.   '        DoA
  69.   '      CASE "B"
  70.   '        DoB
  71.   '      END SELECT
  72.   '  NEXT i
  73.   'END DO
  74.   'END SUB
  75.