Announcement

Collapse
No announcement yet.

Switches

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

  • Switches

    Is there a way to pass arguments (set switches) to a compiled
    program from the comand line, i.e. the /MBR switch to the DOS
    Fdisk program, or the /y switch to Xcopy? I know it can be done
    in C++ but have not seen any references for this in Power Basic.
    Comments? Suggestions?


    ------------------

  • #2
    You can retrieve the entire command line in PowerBASIC using the
    COMMAND$ function.

    Code:
    A$ = COMMAND$
    PRINT "The Command Line was : " & A$
    END
    You can then use functions like INSTR to separate multiple switches.


    Scott


    ------------------
    Scott Slater
    Summit Computer Networks, Inc.
    www.summitcn.com

    Comment


    • #3
      Here are a couple of functions that make life easier when
      dealing with the command line and especially when dealing
      with multiple arguments.


      ' gets the command line and sticks it, separated by spaces,
      ' into array ar$(). returns the number of elements in the array
      '
      FUNCTION cmdline( ar$() ) LOCAL PUBLIC

      x$ = ltrim$( rtrim$( command$ ) )
      a = asplit( x$, ar$() )
      function = a

      END FUNCTION


      ' splits string text$ by spaces and puts them into an array
      ' ar$() and returns the number of elements. Quoted items are
      ' returned as one item. For example:
      ' file1 file2 'long file name'
      ' would return:
      ' file1
      ' file2
      ' 'long file name'

      FUNCTION asplit(BYVAL text$, ar$() ) LOCAL PUBLIC

      text$=ltrim$(rtrim$(text$))
      if text$="" then function=0:exit function
      while 1
      tlen=len(text$)
      cl=instr(text$," ")
      za$=left$(text$,1)
      if za$=chr$(34) or za$="'" or za$="`" then
      z2$=za$
      ncl=instr(2, text$, z2$ )
      if ncl<>0 then cl=ncl
      tp$=ltrim$(rtrim$(left$(text$, cl) ))

      else
      tp$=ltrim$(rtrim$(left$(text$,cl)))
      end if
      if cl=0 then tp$=text$:text$=""

      incr al
      ar$(al)=tp$
      text$=ltrim$(rtrim$(right$(text$,tlen-cl)))
      if text$="" then exit loop
      wend
      asplit=al

      END FUNCTION




      ------------------

      Comment


      • #4
        For simple "switches", such as /F, /Q, etc, a simple INSTR() evaluation is usually quite sufficient.
        Code:
        IF INSTR(UCASE$(COMMAND$), "/Q") THEN ' /Q switch present
        ------------------
        Lance
        PowerBASIC Support
        mailto:[email protected][email protected]</A>
        Lance
        mailto:[email protected]

        Comment


        • #5
          ...or

          if TALLY(lcase$(command$), "/q") then '/q switch present...

          Amos

          ------------------
          Amos

          Comment


          • #6
            Getting picky here, but in this instance, TALLY() would be slightly less efficient, since it has to parse the entire string. INSTR() would stop as soon as it finds an occurrance.



            ------------------
            Lance
            PowerBASIC Support
            mailto:[email protected][email protected]</A>
            Lance
            mailto:[email protected]

            Comment


            • #7
              The speed of parsing the command line is unlikely to be an issue.
              Besides, you really ought to be parsing it one character at a time,
              so you don't mistake a Unix-style path like /lecture/Question.txt
              for a /Q switch!

              It might be worth noting that every Microsoft OS since, I think,
              DOS 2.0 has supported using "/" delimiters as well as "\" delimiters
              for subdirectories. The command shell doesn't always support this
              for its own commands, but the OS' interrupts or API calls do. A
              first-class program should allow for this possibility. (In which
              case, if you expect switches, you should expect that they may be
              started off with a hyphen "-" as well as a "/").

              ------------------
              Tom Hanlin
              PowerBASIC Staff

              Comment


              • #8
                I did say <U>simple</U> switches...

                ------------------
                Lance
                PowerBASIC Support
                mailto:[email protected][email protected]</A>
                Lance
                mailto:[email protected]

                Comment

                Working...
                X