libpnicore
cli_args.hpp
1 //
2 // (c) Copyright 2012 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpnicore.
5 //
6 // libpnicore is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // libpnicore is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with libpnicore. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // ============================================================================
20 //
21 // Created on: Dec, 27 2012
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 #pragma once
25 
26 #include <iostream>
27 #include <vector>
28 #include "../types.hpp"
29 
30 extern "C"
31 {
32 #include<string.h>
33 }
34 
35 namespace pni{
36 namespace core{
37 
38 
48 
62  class cli_args
66  {
67  private:
69  int _argc;
71  char **_argv;
72 
73  public:
74  //------------------------------------------------------------------
76  template<typename CTYPE> cli_args(const CTYPE &str_cont);
77 
78  //------------------------------------------------------------------
80  ~cli_args();
81 
82  //------------------------------------------------------------------
84  const char **argv()const;
85 
86  //------------------------------------------------------------------
88  int argc() const;
89  };
90 
91  //--------------------------------------------------------------------------
92  template<typename CTYPE> cli_args::cli_args(const CTYPE &str_cont):
93  _argc(str_cont.size()+1),
94  _argv(new char*[str_cont.size()+1])
95  {
96  size_t index = 1;
97  _argv[0] = new char;
98 
99  for(auto iter=str_cont.begin();iter!=str_cont.end();++iter)
100  {
101  //allocate memory
102  _argv[index] = new char[iter->size()+1];
103 
104  //copy string
105  strncpy(_argv[index],iter->c_str(),iter->size()+1);
106 
107  index++; //increment argument index
108  }
109 
110  }
111 
112  //--------------------------------------------------------------------------
122  std::ostream &operator<<(std::ostream &ostr,const cli_args &args);
123 
124 //end of namespace
125 }
126 }
~cli_args()
destructor
int argc() const
get the number of arguments
cli_args(const CTYPE &str_cont)
constructor
Definition: cli_args.hpp:92
class managing CLI options
Definition: cli_args.hpp:65
Definition: add_op.hpp:29
int _argc
number of strings in the array below
Definition: cli_args.hpp:69
const char ** argv() const
get the argument values
std::ostream & operator<<(std::ostream &ostr, const cli_args &args)
cli_args output operator
size_t size(const slice &s)
compute slice size
char ** _argv
pointers to the strings content
Definition: cli_args.hpp:71