command line option: optarg


 
Thread Tools Search this Thread
Top Forums Programming command line option: optarg
# 1  
Old 06-17-2009
MySQL command line option: optarg

Hi

I'm learning how to add in programm another option by command line.
For example in the "my_prog" i want to add a " -k " option and then write a number.
I mean:
Code:
my_prog -k 50

and the i should use the number 50.
I'm reading about getopt_long and optarg, and what i have done in the code is to add k in the getop_long as:
Code:
c = getopt_long (*pargc, argv, "B:T:z:k:gpdht", long_options, &option_index)

and the k option in the switch as:

Code:
    case 'k':
      number_i_want = optarg;
      routine_that_use_number_i_want(number_i_want);
      break;

the program run but it doesn't work appropriately. I think the problem is in optarg that should point to a string.
Anyone can help?

Thanks

D.

-----Post Update-----

Sorry I got it.

All i have to do is translate the char to int with the atoi() function.
Code:
number_i_want=atoi(optarg)

Now seems like works!

D.

Last edited by otheus; 06-18-2009 at 09:42 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. UNIX for Dummies Questions & Answers

Command line / script option to filter a data set by values of one column

Hi all! I have a data set in this tab separated format : Label, Value1, Value2 An instance is "data.txt" : 0 1 1 -1 2 3 0 2 2 I would like to parse this data set and generate two files, one that has only data with the label 0 and the other with label -1, so my outputs should be, for... (1 Reply)
Discussion started by: gnat01
1 Replies

3. Programming

Reading command line arguments and setting up values if option not provided

I have a C++ program. I read command line arguments, but if the value is not supplied, I default or make a calculation. Let's say I set it to a default value. I can code this in several ways. Here I show three ways. What would be the best way for maintaining this code? The program will get very... (2 Replies)
Discussion started by: kristinu
2 Replies

4. Homework & Coursework Questions

trouble understanding file option and command line arguments

Hi, I am creating a program with the C language that simulates the WC command in Unix. My program needs to count lines, bytes and words. I have not added the code to count bytes and words yet. I am having trouble understanding what the file option/flag '-' does. I can not visualize how it moves... (1 Reply)
Discussion started by: heywoodfloyd
1 Replies

5. Shell Programming and Scripting

perl script command line option driven script

could someone show me a sample command line option driven script? i want to see an easy way to write one and how i can execute it using command line options such as typing in read.pl -i <id> -c <cmds> -s <start> -e <end> would read out all the commands run by ID . from start time to... (7 Replies)
Discussion started by: kpddong
7 Replies

6. UNIX for Dummies Questions & Answers

What is a menu or command line option driven script?

i'm confused what this means. i was asked to design a menu or command line option driven script that reads out of a DB and displays info such as read_data.pl -u <user> -e <event> which would print commands run by <user>with the <event> in the db. any suggestions? i've been using... (2 Replies)
Discussion started by: kpddong
2 Replies

7. Shell Programming and Scripting

Remove Command-Line Option from String

I want to add a "-r <remote_host>" option to my ksh script, causing the script to run a script of the same name on the specified remote host. The remote invocation should itself include all the command-line options of the original invocation, less the -r option. For example, this invocation: ... (7 Replies)
Discussion started by: mattmiller
7 Replies

8. Shell Programming and Scripting

$OPTARG changes commandline input

Hi there, I hope, that I do not open an thread, that is already existing, but I didn't found something matching with my problem while searching for problems with "getopts" My problem ist, that I'm taking arguments from commandline into my script with getopts, I've an flag -s, after that there... (2 Replies)
Discussion started by: thommes_pommes
2 Replies

9. Shell Programming and Scripting

OPTARG is not taking SPACE

Hi I have below code in one of my shell script: if ; then fail $USAGE; fi while getopts hz:r:t:dz: o do case "$o" in h) echo $USAGE ; exit 0;; r) export REQ_ID="$OPTARG";; t) TIMESPAN="$OPTARG";; d) detail="true";; ) ... (0 Replies)
Discussion started by: mohsin.quazi
0 Replies

10. Shell Programming and Scripting

Usage of optarg

Hello Friends, I need to pass arguments to a shell scripts. but for this i need to take the arguments only if they are supplied to the script as arguments. Like : Prompt > scriptname -d device_cd -s message so how do i capture these arguments ?> i think we have to use optarg.but i dont... (5 Replies)
Discussion started by: sveera
5 Replies
Login or Register to Ask a Question