?
Solved

What do I put in ParseParam?

Posted on 2005-05-13
7
Medium Priority
?
1,149 Views
Last Modified: 2013-11-20
I'm trying to convert my MFC to accept command line args so I do this,

CCommandLineInfo cmdInfo;
ParseCommandLine( cmdInfo );
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;

now I'm having trouble figuring out how to do the last line
cmdArg = cmdInfo.ParseParam(???????);

I want to read in a string, an integer, and a couple of LONGINTS.

Thanks
0
Comment
Question by:zstafa
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 13996244
There's a nice Article on CodeProject that illustrates how to do that: http://www.codeproject.com/library/PlgInArchSingInst.asp ("MFC extension library : Single instance Plug-In"), in particular http://www.codeproject.com/library/PlgInArchSingInst.asp#process ("Processing the new command line")

void CSIPlugIn::ParseNewCommandLine(CCommandLineInfo& rCmdInfo)
{
    for (int i = 0; i < m_commandLineParameters.size(); i++)
    {
        BOOL bFlag = FALSE;
        BOOL bLast = ((i + 1) == m_commandLineParameters.size());
        if (m_commandLineParameters[i].GetAt(0) == '-'
             || m_commandLineParameters[i].GetAt(0) == '/')
        {
            m_commandLineParameters[i] =
m_commandLineParameters[i].Right(m_commandLineParameters[i].GetLength() - 1);
            // remove flag specifier
            bFlag = TRUE;
        }
        rCmdInfo.ParseParam(m_commandLineParameters[i], bFlag, bLast);
    }
}

0
 
LVL 3

Author Comment

by:zstafa
ID: 13996458
jkr that is for a new command line instance correct? I just need to do this once at the beginning. Thanks
0
 
LVL 86

Expert Comment

by:jkr
ID: 13997221
>>I just need to do this once at the beginning

Yes, but the principle remains the same, regardless of when you do that.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:zstafa
ID: 13997639
Is there any way to do this with the current direction I am going or do I have to use this plugin?
0
 
LVL 86

Expert Comment

by:jkr
ID: 13998049
You should just use the part of the plugin's code that does the parameter parsing - no need to use the whole thing.
0
 
LVL 3

Author Comment

by:zstafa
ID: 13998548
In your above class where did you get the CSIPlugIn from? Could you give a little more detail please. Thanks
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13998732
You can download the sample code from http://www.codeproject.com/library/PlgInArchSingInst/SingleInstance.zip

Then, you extract the functions neede from the code and add them tou your app:

void CMyWinApp::ParseCommandLine(CCommandLineInfo& rCmdInfo) // overriding 'CWinApp::ParseCommandLine'
{
   for (int i = 0; i < m_commandLineParameters.size(); i++)
   {
       BOOL bFlag = FALSE;
       BOOL bLast = ((i + 1) == m_commandLineParameters.size());
       if (m_commandLineParameters[i].GetAt(0) == '-'
             || m_commandLineParameters[i].GetAt(0) == '/')
       {
           m_commandLineParameters[i] =
m_commandLineParameters[i].Right(m_commandLineParameters[i].GetLength() - 1);
           // remove flag specifier
           bFlag = TRUE;
       }
       rCmdInfo.ParseParam(m_commandLineParameters[i], bFlag, bLast);
   }
}


void CMyWinApp::InitCommandLineParameters(CString command)
{
    TCHAR* pArgs = command.GetBuffer(command.GetLength() + 1);
    CString parameter;
    bool bInQuotes = false;
    enum {TERM  = '\0', QUOTE = '\"'};
   
    // clear any previous command line parameters
    m_commandLineParameters.clear();
   
    while (*pArgs)
    {
        while (isspace(*pArgs)) // skip leading whitespace
        {
            pArgs++;
        }
       
        bInQuotes = (*pArgs == QUOTE);  // see if this token is quoted
       
        if (bInQuotes) // skip leading quote
        {
            pArgs++;
        }
       
        parameter = "";
       
        // Find next token.
        // NOTE: Args are normally terminated by whitespace, unless the
        // arg is quoted.  That's why we handle the two cases separately,
        // even though they are very similar.
        if (bInQuotes)
        {
            // find next quote followed by a space or terminator
            while (*pArgs && !(*pArgs == QUOTE && 
                  (isspace(pArgs[1]) || pArgs[1] == TERM)))
            {
                parameter += *pArgs;
                pArgs++;
            }
            if (*pArgs == QUOTE)
            {
                pArgs++;
                if (pArgs[1])   // if quoted token not followed by a terminator
                {
                    pArgs += 2; // advance to next token
                }
            }
        }
        else
        {
            // skip to next non-whitespace character which delimts a file
            while (*pArgs)
            {
                if (isspace(*pArgs))
                {
                    // check to see if it correctly delimits an existing file
                    CFile file;
                    if (file.Open(parameter, CFile::modeRead))
                    {
                        // file exists, break the line here
                        file.Close();
                        break;
                    }
                    else
                    {
                        // file does not exist, continue parsing the line
                    }
                }
                parameter += *pArgs;
                pArgs++;
            }
            if (*pArgs && isspace (*pArgs)) // end of token
            {
                pArgs++;         // advance to next token or terminator
            }
        }
        if (!parameter.IsEmpty())
        {
            // we parsed a parameter, push onto list
            m_commandLineParameters.push_back(parameter);
        }
    }
    command.ReleaseBuffer(-1);
}

All that's left to do is adding

vector<CString> m_commandLineParameters;

to your class' declaration
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month9 days, 15 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question