# command line option: optarg

**URL:** <https://community.unix.com/t/command-line-option-optarg/242672>\
**Category:** Programming\
**Created:** [June 17, 2009, 6:12am UTC](https://community.unix.com/t/command-line-option-optarg/242672 "2009-06-17T06:12:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dedalus](https://community.unix.com/letter_avatar/dedalus/32/5_5575768a8748004e209b776fc1b2916d.png) [@Dedalus](https://community.unix.com/u/Dedalus)\
**Post date:** [June 17, 2009, 6:12am UTC](https://community.unix.com/t/command-line-option-optarg/242672/1 "2009-06-17T06:12:18Z")

</div>

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:

```nohighlight
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:

```nohighlight
c = getopt_long (*pargc, argv, "B:T:z:k:gpdht", long_options, &option_index)

```

and the k option in the switch as:

```nohighlight
    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.

```nohighlight
number_i_want=atoi(optarg)

```

Now seems like works!

D.
