473,226 Members | 1,382 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 software developers and data experts.

Command line parameters - argc and argv[]

ern
Is there a standard (or easy) way to parse parameters (configs) that
come from main(int argc, char *argv[]) ?

For example,

If I have the following configurations:

-s <string // pass a string to main

-f <file // pass a file to main

-i <integer // pass an integer to main

-d // set the 'doStuff' flag

as well as being able to send any arbitrary command to main(), are
there any proven methods of parsing the configurations? It seems like
option-parsing code would already exist, but I can't find any. The
option-parsing in the code I'm debugging is messy and unacceptable.
I'd like to start from scratch. For Python minded people, I'm looking
for an equivalent to 'optparse'.

Aug 17 '06 #1
2 3502
ern <er*******@gmail.comwrote:
Is there a standard (or easy) way to parse parameters (configs) that
come from main(int argc, char *argv[]) ?
There's nothing included in the standard library, although it's
certainly possible to implement something that does this in standard
C. If you're on a Unix system, check out getopt(); you can get more
help with that on comp.unix.programmer.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Aug 17 '06 #2
In article <11**********************@i3g2000cwc.googlegroups. com>,
ern <er*******@gmail.comwrote:
>Is there a standard (or easy) way to parse parameters (configs) that
come from main(int argc, char *argv[]) ?
>For example,
If I have the following configurations:
-s <string // pass a string to main
It isn't part of the C language itself, but many systems
provide routines named getopt() or similar; there are fully
portable versions of the routine readily available.

Different option parsers offer different facilities. For example,
many of the getopt() routines do not offer a mechanism to type-check
provided arguments, nor to convert the provided argument from
string to integer (or float, or enumeration type, or whatever): they
just break out the strings and let you deal with the semantics.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Aug 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bernd Danberg | last post by:
Hi, I have a real strange problem with the command line arguments given to the main-function and together with using std::string: #include <string> int _tmain(int argc, _TCHAR* argv) {...
7
by: qazmlp | last post by:
void func() { // Is it by anyway possible to read the value of the first command line parameter i.e. argv here ? } int main() { func() ; // No command line arguments are passed to func(). }
25
by: David Bernier | last post by:
I'd like to pass on the command line two filenames. As for example: my_executable filename_1 filename_2 I haven't done any C programming with command line arguments so far. I'm familiar...
19
by: linzhenhua1205 | last post by:
I want to parse a string like C program parse the command line into argc & argv. I hope don't use the array the allocate a fix memory first, and don't use the memory allocate function like malloc....
2
by: SunRise | last post by:
Hi I am creating a C Program , to extract only-Printable-characters from a file ( any type of file) and display them. OS: Windows-XP Ple help me to fix the Errors & Warnings and explain...
34
by: Roman Mashak | last post by:
Hello, All! I'm implementing simple CLI (flat model, no tree-style menu etc.). Command line looks like this: <command> <param1> <param2> ... <paramN> (where N=1..4) And idea is pretty simple: ...
3
by: kitty | last post by:
Can i provide for command line switches in c++ ? For example i can say perl readfile.pl -f filename, Is g++ readfile.cc -f filename possible ? Thankyou for all you help,.
40
by: raphfrk | last post by:
I have a program which reads in 3 filenames from the command line prog filename1 filename2 filename3 However, it doesn't work when one of the filenames has spaces in it (due to a directory...
1
by: vutek0328 | last post by:
I have a code of which I need to set parameters in order to run. I'm wondering how I can input the parameters through the command line. ./brownian 10 15 100 200 with each number being a...
17
by: Matt | last post by:
Hello. I'm having a very strange problem that I would like ot check with you guys. Basically whenever I insert the following line into my programme to output the arguments being passed to the...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.