What is the safest way to make an argv[] comparison? The code below
works.
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string test;
if(argc>1)
test = argv[1];
if(test=="NKDT" )
cout << "\nComparis on is true. Have program do something.\n";
else
cout << "\nComparis on is false. Have program do something else.\n";
return 0;
}
May 21 '07
28 5800
Alf P. Steinbach wrote:
#include <iostream>
#include <cstddef>
#include <string>
#include <vector>
#include <stdexcept>
typedef std::vector<std ::stringStringV ector;
bool throwX( char const s[] ) { throw std::runtime_er ror( s ); }
void cppMain( StringVector const& arguments )
{
arguments.size( ) 1
|| throwX( "usage: myprog ARG1" );
if( arguments.at(1) == "NKDT" )
{
// Do something.
}
else
{
// Do something else.
}
}
int main( int n, char* a[] )
{
try
{
cppMain( StringVector( a, a+n ) );
return EXIT_SUCCESS;
}
catch( std::exception const& x )
{
std::cerr << "!" << x.what() << std::endl;
return EXIT_FAILURE;
}
}
Why such a complicated solution to such a simple problem? How about
simply:
int main(int argc, char* argv[])
{
std::vector<std ::stringcmdLine (argv, argv+argc);
if(cmdLine.size () 1 && cmdLine[1] == "NKDT")
{
// Do something
}
else
{
// Do something else
}
}
On May 22, 7:57 am, Gianni Mariani <gi3nos...@mari ani.wswrote:
dave_mikes...@f astmail.fm wrote:
std::vector is part of the standard.
std::string is part of the standard.
What about these is complex ?
There exists a minimum complexity. employing char * is usually bad news
and is prone to far more subtle complexity that is exemplified by the
OP's question.
Not sure what you mean here - the OP's solution creates a std::string
from argv[1]. If he only cares about argv[1], his solution seemed
fine to me re: parsing the command line.
On May 22, 7:57 am, Gianni Mariani <gi3nos...@mari ani.wswrote:
dave_mikes...@f astmail.fm wrote:
std::vector is part of the standard.
std::string is part of the standard.
What about these is complex ?
There exists a minimum complexity. employing char * is usually bad news
and is prone to far more subtle complexity that is exemplified by the
OP's question.
Not sure what you mean here - the OP's solution creates a std::string
from argv[1]. If he only cares about argv[1], his solution seemed
fine to me re: parsing the command line.
* Juha Nieminen:
Alf P. Steinbach wrote:
> #include <iostream>
#include <cstddef> #include <string> #include <vector> #include <stdexcept>
typedef std::vector<std ::stringStringV ector;
bool throwX( char const s[] ) { throw std::runtime_er ror( s ); }
void cppMain( StringVector const& arguments ) { arguments.size( ) 1 || throwX( "usage: myprog ARG1" );
if( arguments.at(1) == "NKDT" ) { // Do something. } else { // Do something else. } }
int main( int n, char* a[] ) { try { cppMain( StringVector( a, a+n ) ); return EXIT_SUCCESS; } catch( std::exception const& x ) { std::cerr << "!" << x.what() << std::endl; return EXIT_FAILURE; } }
Why such a complicated solution to such a simple problem? How about
simply:
int main(int argc, char* argv[])
{
std::vector<std ::stringcmdLine (argv, argv+argc);
if(cmdLine.size () 1 && cmdLine[1] == "NKDT")
{
// Do something
}
else
{
// Do something else
}
}
Yours is not reusable without changes. Note that the code you put in
'main' is the code in 'cppMain' (and all code that is program-specific,
i.e. that has to be written), except that 'cppMain' is safer because it
doesn't give access to the C-style arguments and because it can safely
throw exceptions. Always think about reusing code instead of inventing
the wheel over and over with just slight details different.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
* Lionel B:
On Mon, 21 May 2007 20:39:14 +0200, Alf P. Steinbach wrote:
>* ka*****@hotmail .com:
>>What is the safest way to make an argv[] comparison? The code below works.
#include <iostream> #include <string>
using namespace std;
int main(int argc, char *argv[]) { string test;
if(argc>1) test = argv[1];
if(test=="NKDT" ) cout << "\nComparis on is true. Have program do something.
\n";
>> else cout << "\nComparis on is false. Have program do something
else.\n";
>> return 0; }
Do the following:
[snip]
You appear to assume that an exception should be thrown if the command
line does not have a particular form. I see no such requirement expressed
by the OP who does not, in fact, suggest that the "false" comparison is
in any way "exceptiona l".
No, using an exception is just the simple and safe way to do things in
that context.
On the other hand, I do assume that any actual program will need to deal
with exceptions; otherwise you're using C++ as just a better C.
There are more options for that than just the code shown, but if you're
going to write a little toy program it's good boilerplate code.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
On May 22, 7:57 am, Gianni Mariani <gi3nos...@mari ani.wswrote:
dave_mikes...@f astmail.fm wrote:
std::vector is part of the standard.
std::string is part of the standard.
What about these is complex ?
There exists a minimum complexity. employing char * is usually bad news
and is prone to far more subtle complexity that is exemplified by the
OP's question.
Not sure what you mean here - the OP's solution creates a std::string
from argv[1]. If he only cares about argv[1], his solution seemed
fine to me re: parsing the command line.
On Tue, 22 May 2007 17:04:34 +0200, Alf P. Steinbach wrote:
* Lionel B:
>On Mon, 21 May 2007 20:39:14 +0200, Alf P. Steinbach wrote:
>>* ka*****@hotmail .com: What is the safest way to make an argv[] comparison? The code below works.
#include <iostream> #include <string>
using namespace std;
int main(int argc, char *argv[]) { string test;
if(argc>1) test = argv[1];
if(test=="NKDT" ) cout << "\nComparis on is true. Have program do something.
\n";
>>> else cout << "\nComparis on is false. Have program do something
else.\n";
>>> return 0; } Do the following:
[snip]
You appear to assume that an exception should be thrown if the command line does not have a particular form. I see no such requirement expressed by the OP who does not, in fact, suggest that the "false" comparison is in any way "exceptiona l".
No, using an exception is just the simple and safe way to do things in
that context.
Well, it is *a* simple(ish) and safe way.
On the other hand, I do assume that any actual program will need to deal
with exceptions;
Beside the point... the fact remains that (as far as we can know) the
"false" branch in the OP's code may be perfectly "unexceptional" . In fact
his/her action for the "false" branch was "Have program do something
else". That doesn't (to me) say "throw exception".
otherwise you're using C++ as just a better C.
A perfectly honourable use for C++ ;-)
There are more options for that than just the code shown, but if you're
going to write a little toy program it's good boilerplate code.
Sure, it's a fine idiom if deployed as the OP intended, which I'm not
convinced it was.
--
Lionel B
* Lionel B:
>
Beside the point... the fact remains that (as far as we can know) the
"false" branch in the OP's code may be perfectly "unexceptional" . In fact
his/her action for the "false" branch was "Have program do something
else". That doesn't (to me) say "throw exception".
I'm not sure that the effect of the OP's code was intentional, and the
question was how to deal with argv, not how to replicate the effect of
the original code (still I posted such replication else-thread).
Anyway, it's silly and just plain argumentative to object to getting too
generally useful code.
If you'd objected that the code was too special purpose it could have
been a valid objection.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alf P. Steinbach wrote:
Yours is not reusable without changes.
"My" version consists basically of two (2) lines: One line which
creates the vector, and another with compares the first command-line
argument. Your requirement of "reusabilit y" for two lines of code seems
quite odd to me. It doesn't make too much sense.
As for "without changes", that's even odder. Your solution requires
exactly as many changes to be "reusable" as mine, namely changing the
string being compared.
Note that the code you put in
'main' is the code in 'cppMain' (and all code that is program-specific,
i.e. that has to be written), except that 'cppMain' is safer because it
doesn't give access to the C-style arguments and because it can safely
throw exceptions.
Do you understand the concept of over-engineering?
Always think about reusing code instead of inventing
the wheel over and over with just slight details different.
Having to write 2 lines of code is "inventing the wheel over and
over". Right.
* Juha Nieminen:
Alf P. Steinbach wrote:
>Yours is not reusable without changes.
"My" version consists basically of two (2) lines: One line which
creates the vector, and another with compares the first command-line
argument. Your requirement of "reusabilit y" for two lines of code seems
quite odd to me. It doesn't make too much sense.
Yes, it's true, what you're saying doesn't make sense. And the reason
that what you're saying doesn't make sense, is that you have based your
reasoning on false assumptions in order to fit a preconceived senseless
conclusion. The reusability is not for two problem-specific lines of
code: it's for argument packaging, exception handling (more generally
failure handling), main return codes, and removal of access to low-level
stuff.
And that's everything except the code between the braces in 'cppMain'.
Not that your code is much inferior, since we're talking about two
extremely trivial and short programs, but it's based on reinventing the
wheel. Except if you like spaghetti and bug-fixing, the above has to
be done for any program that does anything real. Even though we're
still talking about novice programs, toy programs, small test programs.
As for "without changes", that's even odder. Your solution requires
exactly as many changes to be "reusable" as mine, namely changing the
string being compared.
That's incorrect. And the reason it's incorrect is that you envision a
specific case of reuse where your code conceivably could be reused
mostly as-is, in order to fit your preconceived incorrect conclusion.
Out of the infinitely many programs one can make you've chosen an
extremely small set parameterized by one datum, and you incorrectly call
that reusability.
> Note that the code you put in 'main' is the code in 'cppMain' (and all code that is program-specific, i.e. that has to be written), except that 'cppMain' is safer because it doesn't give access to the C-style arguments and because it can safely throw exceptions.
Do you understand the concept of over-engineering?
> Always think about reusing code instead of inventing the wheel over and over with just slight details different.
Having to write 2 lines of code is "inventing the wheel over and
over". Right.
I'm sorry, what you're writing is not meaningful.
Nobody forces you to use save work by using that little boilerplate code.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Robin Sanderson |
last post by:
Sorry in advance if this is a stupid question - I am new to C++.
In the process of converting program to be run from the command line
into a function to be run from another program I noticed behaviour
that I do not understand. Consider the example programs below:
Program 1 below is a simple program that merely outputs the command
line arguments. This compiles and runs fine with Microsoft Visual C++
6.0 and g++ 3.3.1.
|
by: mahurshi |
last post by:
i have a quick question
i am putting a debug flag in my program (i really dont need this
feature, but i figured it might be useful when i get into trouble)
so i want to check if argv is the letter "d"
this is what i have so far
if (argv) { write_read_input_file(filename); }
|
by: Charles Sullivan |
last post by:
I'm working on a program which has a "tree" of command line arguments,
i.e.,
myprogram level1 ]]
such that there can be more than one level2 argument for each level1
argument and more than one level3 argument for each level2 argument, etc.
Suppose I code it similar to this fragment:
int main( int argc, char *argv )
{
|
by: James |
last post by:
consider :
int main (int argc, char * argv ) {
}
In exec (2) ; Whose arguments which are passed to main.
What is the maximum size of the string.
argv = "hello........." ;
|
by: jab3 |
last post by:
(again :))
Hello everyone.
I'll ask this even at risk of being accused of not researching adequately.
My question (before longer reasoning) is: How does declaring (or defining,
whatever) a variable **var make it an array of pointers?
I realize that 'char **var' is a pointer to a pointer of type char (I hope).
And I realize that with var, var is actually a memory address (or at
| |
by: Joe Smith |
last post by:
It is nothing short of embarrassing to feel the need to ask for help on
this. I can't see how I would make the main control for this. What I want
is a for loop and a test condition. And while I know, from things I
pondered 2 decades ago, that a fella can write code without a goto, I'm
stuck.
/* sieve1.c */
#define whatever 20
#define N whatever
|
by: interec |
last post by:
Hi Folks,
I am writing a c++ program on redhat linux using main(int argc,
wchar_t *argv). $LANG on console is set to "en_US.UTF-8". g++
compiler version is 3.4.6.
Q1. what is the encoding of data that I get in argv ?
Q2. what is encoding of string constants defined in programs (for
example L"--count") ?
|
by: vicky |
last post by:
hi all,
please tell me with example, how the *argv point to the the no of
strings.
|
by: Romulo Carneiro |
last post by:
Hi,
I'm programming in Windows XP and i'm trying to get all arguments of
some application, but i only have gotten five argv. When i put more
then five(5), it didn't display.
=>Input Command Line:
Argumentos.exe MyName Arg_1 Arg_2 Arg_3 Arg_3 Arg_4 Arg_5 Arg_6
=>output of my program:
Size of Argc:8
Size of Argv: 4
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |