1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
/**
* $Id$
*
* ACE_ARGV examples not in a larger program. Sample code from The ACE
* Programmer's Guide, Copyright 2003 Addison-Wesley. All Rights Reserved.
*/
#include "ace/os_include/os_netdb.h"
#include "ace/OS_NS_string.h"
#include "ace/Log_Msg.h"
// Listing 1 code/ch04
#include "ace/ARGV.h"
#include "ace/Get_Opt.h"
int ACE_TMAIN (int, ACE_TCHAR *[])
{
static const ACE_TCHAR options[] = ACE_TEXT (":f:h:");
static const ACE_TCHAR cmdline[] =
ACE_TEXT ("-f /home/managed.cfg -h $HOSTNAME");
ACE_ARGV cmdline_args (cmdline);
ACE_Get_Arg_Opt<ACE_TCHAR> cmd_opts (cmdline_args.argc (),
cmdline_args.argv (),
options,
0); // Don't skip any args
// Listing 1
int option;
ACE_TCHAR config_file[MAXPATHLEN];
ACE_TCHAR hostname[MAXHOSTNAMELEN];
ACE_OS_String::strcpy (config_file, ACE_TEXT ("HAStatus.conf"));
ACE_OS_String::strcpy (hostname, ACE_TEXT ("not set"));
while ((option = cmd_opts ()) != EOF)
switch (option) {
case 'f':
ACE_OS_String::strncpy (config_file,
cmd_opts.opt_arg (),
MAXPATHLEN);
break;
case 'h':
ACE_OS_String::strncpy (hostname,
cmd_opts.opt_arg (),
MAXHOSTNAMELEN);
break;
case ':':
ACE_ERROR_RETURN
((LM_ERROR, ACE_TEXT ("-%c requires an argument\n"),
cmd_opts.opt_opt ()), -1);
default:
ACE_ERROR_RETURN
((LM_ERROR, ACE_TEXT ("Parse error.\n")), -1);
}
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Config file: %s\n"), config_file));
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Hostname: %s\n"), hostname));
return 0;
}
|