Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Parsing COMMAND$

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PBDos Parsing COMMAND$

    While going through the alt.lang.powerbasic newsgroup last night, I came across an post, ca. 2003, where someone was wondering how to parse the COMMAND$ variable for multiple options. As the original posting was for two parameters, I came up with some code to help get the original poster started and I thought I would share it here as well:

    s$ = UCASE$(COMMAND$) 'get the files you want to process
    sp = instr(s$, " ") 'Get the position of the space character
    i$ = left$(s$, len(s$) - sp) 'Pull the name of the input file
    o$ = right$(s$, len(s$) - sp) 'Pull the name of the output file
    print "input file: "; i$ 'Show the name of the input file
    print "output file: "; o$ 'Show the name of the output file

  • #2
    Hi Robert!
    I'm not sure if you knew, but the PowerBASIC COMMAND$ statement already allows you to parse the incoming arguments ...

    Code:
    s$ = Command$(2)

    Comment


    • #3
      File names can contain spaces
      Delimiter may not be a space
      Upper and lower case must be considered

      Code:
      FUNCTION PBMAIN () AS LONG
       LOCAL s,sArray(),sDelimiter AS STRING,x, items AS LONG
       sDelimiter = "/"
       s = "How/now/brown cow"  'could be s=COMMAND$
       items = Parser(s,sArray(),sDelimiter)
       FOR x = 1 TO items
        ? sArray(x),,USING$("item # of #",x,items)  'process each delimited field
       NEXT
      END FUNCTION
      
      FUNCTION Parser(s AS STRING,sArray() AS STRING,sDelimiter AS STRING) AS LONG
       LOCAL items AS LONG
       items = PARSECOUNT(s,sDelimiter)
       DIM sArray(1 TO Items)
       PARSE s,sArray(),sDelimiter
       FUNCTION = UBOUND(sArray)
      END FUNCTION
      https://duckduckgo.com instead of google

      Comment


      • #4
        Or, put arguements containing spaces in double quotes. (at least in PBWin or PBCC)
        Dale

        Comment


        • #5
          Originally posted by Gary Beene View Post
          Hi Robert!
          I'm not sure if you knew, but the PowerBASIC COMMAND$ statement already allows you to parse the incoming arguments ...

          Code:
          s$ = Command$(2)
          Gary

          I use it all the time

          I think it might be a statement added in recent versions of the compiler - is that right?

          Kerry
          [I]I made a coding error once - but fortunately I fixed it before anyone noticed[/I]
          Kerry Farmer

          Comment


          • #6
            PBWin 8 and PBCC 4 do not have COMMAND$(argnum), just COMMND$
            PBWin 9/10 and PBCC 5/6 do have the parse option.

            Cheers,
            Dale

            Comment


            • #7
              This was for PBDos as per OP {:-)
              <b>George W. Bleck</b>
              <img src='http://www.blecktech.com/myemail.gif'>

              Comment


              • #8
                Originally posted by George Bleck View Post
                This was for PBDos as per OP {:-)
                Yep, and I'm sure that after 15 years the original poster was really happy to get an answer

                Comment


                • #9
                  Originally posted by Stuart McLachlan View Post

                  Yep, and I'm sure that after 15 years the original poster was really happy to get an answer
                  I was just happy that I _COULD_ give an answer. If the poster of that message did not get it, then maybe someone else will find it useful.

                  Comment


                  • #10
                    I've been using this little bit of code for a long time to parse command line parameters
                    Code:
                    #COMPILE EXE
                    #DEBUG ERROR ON
                    #DIM ALL
                    
                    FUNCTION PBMAIN() AS LONG
                      LOCAL strCommand AS STRING
                      LOCAL strDate AS STRING
                      LOCAL strTime AS STRING
                      LOCAL strPath AS STRING
                      '
                      strCommand = COMMAND$        ' pick up the command line
                      '
                      ' for testing purposes set the strCommand values - note that " is escaped just for purposes of building the string
                      ' command string would be  /Date#"01/06/2004" /Time#"22:00" /Path#"*.*""
                      strCommand = "/Date#""01/06/2004"" /Time#""22:00"" /Path#""*.*"""
                      '
                      CON.STDOUT strCommand
                      CON.STDOUT funReturnNamedParameterEXP("/Date#",strCommand)    ' Parameter names are case independant
                      CON.STDOUT funReturnNamedParameterEXP("/TIME#",strCommand)
                      CON.STDOUT funReturnNamedParameterEXP("/pAtH#",strCommand)
                      '
                      CON.STDOUT "Press any key to exit"
                      WAITKEY$
                    
                    END FUNCTION
                    
                    
                    FUNCTION funReturnNamedParameterEXP(strName AS STRING, strCommand AS STRING) AS STRING
                    ' return the named parameter where strCommand is in the form
                    ' /D#"01/06/2004" /T#"22:00" /P#"*.*"
                    ' where strName = /D#
                      DIM str_funRNP_EXP_Result AS STRING
                      DIM lng_funReturnNamedParameterEXP_Start AS LONG
                      DIM strLocalCommand AS STRING
                      '
                      strLocalCommand = UCASE$(strCommand)  ' store local, case independant, version of command string
                      '
                      ' now return the parameter where the strName is a case independant search
                      strName = UCASE$(strName)
                      '
                      lng_funReturnNamedParameterEXP_Start = INSTR(strLocalCommand,strName)
                      '
                      IF lng_funReturnNamedParameterEXP_Start>0 THEN
                      ' only if a match has been found
                        str_funRNP_EXP_Result = MID$(strCommand,lng_funReturnNamedParameterEXP_Start)
                        str_funRNP_EXP_Result = RIGHT$(str_funRNP_EXP_Result,LEN(str_funRNP_EXP_Result)-LEN(strName))
                        str_funRNP_EXP_Result = PARSE$(str_funRNP_EXP_Result,"""",2)
                        str_funRNP_EXP_Result = REMOVE$(str_funRNP_EXP_Result,"""")
                        '
                      ELSE
                        str_funRNP_EXP_Result = ""
                      END IF
                      '
                      FUNCTION = str_funRNP_EXP_Result
                    END FUNCTION

                    Comment

                    Working...
                    X