Ancient Dragon was helpful in a way to parse argv(). It brought to mind
a method that suggested to me a way to look at the prefix after finding the expected string. For example (as I interpret it) if you are looking for a prefixed command it could be a command or it might something else like a file name. So for example if a command you are looking at is "key" and it comes in as "-key" then if you detect the word "key" you might check to see if the previous character was "-". In the following
code as a kind of example suggests a way to do this:

// tesstrcmp.cpp : Defines the entry point for the console application.

// ArgParse: an entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#define STRINGSIZE 128
int main(int argc, char *argv[])
// char ch;
// char *str;
int c = 1;
	while (c < argc) {
		printf("arg %i - |%s|\n",c,argv[c]);
		if (argv[c][0] == '-') {
			printf("|%c|\n", argv[c][0]);
                        // then show what it is
			// assume the option string immediately
                        // follows the - symbol
			if( strcmp( &argv[c][1], "key" ))
				printf("%s\n", &argv[c][1]);

This code will print out the "-" if it precedes the string "key". If "key" was typed and the preceding character was not "-" then using
some similar code, one could determine that the word "key" should probably be treated as a file name or some other case.

Just some random thoughts.

Thanks Ancient Dragon!


Recommended Answers

All 6 Replies

It is customary to have program options to start with either - or /, such as -key or /key. Sometimes you may want an option to be something like "-key=Something", and you may also want to allow for spaces within the string, for example you may type on the commandline prog.exe - key = something That becomes a little more tricky because in that case the value of argc will be 5 instead of 2.

Here's something you may want to look at.

Here's something you may want to look at.


I wonder if there is a way to have both a command line program as this line
of questions implies, and work as a Windows Forms program. In other words, if
the program is started as a console command, use main() and arguments, but
also allow the same code to be started as a windows application in which case,
use openFileDialog and other forms controls and objects.

Can You Have Your Cake and Eat it too?

This would be ideal and it is something that if possible I would like to learn.

I realize that I am a bit of a newbie here, especially with Visual Studio and
I may be trying to move too fast but there is so much good information here in
these forums that I feel like a kid in a candy store.

Thanks WaltP! Cheers!

I don't know for sure. I don't do Windows Forms programs. I think it might be possible.

Here is a link I found how to process command-line arguments in Windows Forms applications.

Here is a link I found how to process command-line arguments in Windows Forms applications.

Thanks Ancient Dragon!

I am will to try to make a Windows forms application as an example
that simply displays a list of files. When invoked as a command line
application with switches it might output a list of files to the
standard output. My goal is to be able to perform various functions
on files as specified by either command line or via a Windows forms

If I come up with something I will post it here.

Thanks again for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.