Re: command line arguments best practices

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"iceman420205" <iceman420205@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:3522ECAD-A691-479F-A86A-5905C5C3D76F@xxxxxxxxxxxxxxxx
> I have seen a number of posts here and on the net as to how to use
Command$.
> I am familiar with how to create a string, set it equal to Command$, and
then
> parse it or do whatever I want.
>
> However, I am curious as to the best practice in controlling more complex
> parameters.
>
> What is the best delimiter (e.g. slash, hyphen, pipe)?
> For example, MyApp.exe /f /q or MyApp.exe -f -q or MyApp.exe f|q.
>
> What is the best indicator (e.g. single or double letters, numbers)?
> For example, MyApp.exe /fs /q
>
> Should the indicator be followed by another character to help the parsing
> (e.g. colon)?
> For example, MyApp.exe /fs:Hello /q
>
> Should quotation marks be used or discouraged?
> For example, MyApp.exe /hHello or MyApp.exe /h"Hello"
>
> Should spaces be used or discouraged?
> For example, MyApp.exe /a 3456 or MyApp.exe /a3456.
>
> What is the best way to handle paths?
> For example, MyApp.exe /fsc:\temp\blah.txt /q
>
> Once the delimiter is determined, what is the best way to parse Command$?
> Dim sCommand as String
> sCommand = Command$
>
> Load string in an array?
> Dim vCommand As Variant
> vCommand = Split(sCommand, "|")
>
> Replace delimiter with something else?
> sCommand = Replace(sCommand, "/", vbCrLf)
>
> Use Select Case?
>
> Write a separate function?
>
> Make a class?
>
> I prefer to use industry standards and to utilize the experience of the
> best. I apologize if this is too broad and/or lengthy an issue, just a
> personal preference, or not the place to have such a discussion. Thank
you
> for your time and efforts.
>
> iceman420205
>

There are three broad 'standards' for managing and defining protocols for
command line parameters. The different scenarios you have above are pieces
from each.

The first is a simple positional parse using the default delimiter. Then
using whatever rules you want for each parameter for further processing. Ad
hoc, frequently used, and error-prone.

The second is getopts() (published and copyrighted by AT&T since time began.
<g>) which is widely used in Unix and C/C++ runtimes and perhaps comes the
closest to being a 'Standard'.
You can look at the definition and the source at...
http://archive.devx.com/sourcebank/directorybrowse.asp?adType=&dir_id=790&showResType=0&timeSelect=&showFilter=showAll
Its in C but should be easy to puzzle it out for your own application. There
a couple of zillion examples out there.

This standard is characterized by allowing single and multiple character
parameters, is non positional, and provides special characters for string
values (':'), and allows the 'flag' to either be a hyphen or a backslash.

I use a getopts() that I have modified for VB for all my programs that take
complex parameters. If you decided to go that route I can help with some
source.

The other 'standard' is loosely term MSDOS/VMS. It is characterized by a
combination of positional and non-positional parameters, case-insensitive,
and using a separate delimiter for strings (the double quote), and a single
flag (the forward slash).

But you will find that outside of the Unix/C world or special heavy iron
environments - it is pretty much of a free-for-all. <g>

I apologize for not giving an explicit answer for most of your questions as
the answer to all would be something like, "Well, that's ok, but I would do
this..." ie, full of personal opinion (although very sage and valuable
opinions, since they are mine. <g>).

You should pick the style you feel would be less confusing for your users
and adequate for your needs and stick with it. Then go surfing. There are a
couple of zillion pieces of source code out there.

hth
-ralph




.



Relevant Pages

  • Re: Read a single byte from stdin
    ... standard input without involving AWT or Swing? ... System streams and Console, but both are doing line buffering and neither ... simulate a console that could read a character at a time. ... real command processor console? ...
    (comp.lang.java.programmer)
  • Re: command line arguments best practices
    ... > Command$. ... >> Once the delimiter is determined, what is the best way to parse ... > closest to being a 'Standard'. ... > This standard is characterized by allowing single and multiple character ...
    (microsoft.public.vb.general.discussion)
  • Re: Interacting with command line app (netsh)
    ... The standard output stream doesn't flush automatically. ... > need to execute a command, write the command to the input (including a ... I then repeat this process one character at a time ... > until Peek fails, at which point I go on and issue the next command. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Quotes nested inside bash
    ... character as the delimiter. ... When run from my shell, the following command works just fine: ...
    (comp.unix.shell)
  • vi editor FAQ (Frequently Asked Question List), Part 2/2
    ... has the UCB distribution of vi, and lots of useful macros. ... m0 is the ex command to move the line to line 0. ... Swap character and one vertically above: ... A non-visual editor under Unix. ...
    (comp.unix.questions)