TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: VIRGIL GARNESS
from: ANTHONY TIBBS
date: 1997-06-17 15:58:00
subject: Passing switches to programs

On Jun 13 16:16, 1997, VIRGIL GARNESS of 2:2501/206 wrote:
VG> Hi All,
VG> How is it possible to pass switches to bypass a Switch in a 
VG> program, say
VG> like from the dos prompt.  I'm trying to make a program that can 
VG> run on
VG> it's own normally with a menu or run in a batch file with switches 
VG> to
VG> bypass the menus.
I'm not sure just how portable this is, but:
int main (int argc, char **argv)
{
    // blah blah
}
"argc" will contain the number of arguments, and argv[] will contain the 
arguments themselves.  For example:
int main (int argc, char **argv)
{
int Count;
    cout << "Not-so-great demonstration of command-line switches" << endl;
    if (argc > 1)
    {
        for (Count = 1; Count <= argc; Count++)
        {
            cout << "Argument # " << Count << " is " << argv[Count] << endl;
        }
    }
    
    return 0;
}
Let's say this compiles to ARG.EXE.  If you type "ARG My Name Is Virgil", the 
program will display
Argument #1 is My
Argument #2 is Name
Argument #3 is Is
Argument #4 is Virgil
However, if you typed ARG "My Name Is Virgil" (with the quotes), it would 
display
Argument #1 is My Name Is Virgil.
Again, I am not completely sure how portable this is, but it worked here.
Anthony
--- TriED 0.90
---------------
* Origin: World of Power BBS (pvt) (1:163/215.38)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.