libyui  3.3.2
YCommandLine.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YCommandLine.h
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #ifndef YCommandLine_h
26 #define YCommandLine_h
27 
28 #include <string>
29 #include "ImplPtr.h"
30 
32 
33 
34 /**
35  * Utility class to access /proc/<pid>/cmdline to retrieve argc and argv
36  **/
38 {
39 public:
40  /**
41  * Constructor. This will read /proc/<pid>/cmdline of this process.
42  **/
43  YCommandLine();
44 
45  /**
46  * Destructor.
47  **/
48  ~YCommandLine();
49 
50  /**
51  * Return the number of arguments in the command line.
52  * Remember that the command itself (the binary of the process) is
53  * included, so a value of 1 (not 0!) means "no additional arguments".
54  **/
55  int argc() const;
56 
57  /**
58  * Return the arguments in a C compatible fashion: An array of pointers to
59  * characters. The data are copied with strdup(), so they are valid beyond
60  * the life time of this object (but OTOH should be released with free() at
61  * some point).
62  **/
63  char ** argv() const;
64 
65  /**
66  * Alias for argc() for those who like a more C++ -like syntax.
67  **/
68  int size() const { return argc(); }
69 
70  /**
71  * Return command line argument no. 'index' (from 0 on).
72  *
73  * This might throw an YUIIndexOutOfRangeException.
74  **/
75  std::string arg( int index ) const;
76 
77  /**
78  * Return command line argument no. 'index' (from 0 on) as operator[]:
79  *
80  * for ( int i=0; i < cmdLine.argc(); i++ )
81  * cout << cmdLine[i] << std::endl;
82  *
83  * This might throw an YUIIndexOutOfRangeException.
84  **/
85  std::string operator[]( int index ) const
86  { return arg( index ); }
87 
88  /**
89  * Add a command line argument (at the end of the existing ones).
90  **/
91  void add( const std::string & arg );
92 
93  /**
94  * Remove command line argument no. 'index' (from 0 on).
95  *
96  * This might throw an YUIIndexOutOfRangeException.
97  **/
98  void remove( int index );
99 
100  /**
101  * Replace command line argument no. 'index' (from 0 on) with 'arg'.
102  *
103  * This might throw an YUIIndexOutOfRangeException.
104  **/
105  void replace( int index, const std::string & arg );
106 
107  /**
108  * Find a command line argument 'argName' ("-display" etc.).
109  * Notice that leading minus signs must be specified in 'argName'.
110  * Since argv[0] is the program name, the search starts from argv[1].
111  *
112  * Return the position of 'argName' (from 0 on) or -1 if not found.
113  **/
114  int find( const std::string & argName ) const;
115 
116 
117 private:
118 
120 };
121 
122 
123 #endif // YCommandLine_h
int find(const std::string &argName) const
Find a command line argument &#39;argName&#39; ("-display" etc.).
~YCommandLine()
Destructor.
Definition: YCommandLine.cc:71
int size() const
Alias for argc() for those who like a more C++ -like syntax.
Definition: YCommandLine.h:68
YCommandLine()
Constructor.
Definition: YCommandLine.cc:48
std::string operator[](int index) const
Return command line argument no.
Definition: YCommandLine.h:85
void replace(int index, const std::string &arg)
Replace command line argument no.
Utility class to access /proc/<pid>/cmdline to retrieve argc and argv.
Definition: YCommandLine.h:37
char ** argv() const
Return the arguments in a C compatible fashion: An array of pointers to characters.
Definition: YCommandLine.cc:85
void add(const std::string &arg)
Add a command line argument (at the end of the existing ones).
int argc() const
Return the number of arguments in the command line.
Definition: YCommandLine.cc:78
std::string arg(int index) const
Return command line argument no.