PCL
Arguments.h
Go to the documentation of this file.
1 // ____ ______ __
2 // / __ \ / ____// /
3 // / /_/ // / / /
4 // / ____// /___ / /___ PixInsight Class Library
5 // /_/ \____//_____/ PCL 2.7.0
6 // ----------------------------------------------------------------------------
7 // pcl/Arguments.h - Released 2024-06-18T15:48:54Z
8 // ----------------------------------------------------------------------------
9 // This file is part of the PixInsight Class Library (PCL).
10 // PCL is a multiplatform C++ framework for development of PixInsight modules.
11 //
12 // Copyright (c) 2003-2024 Pleiades Astrophoto S.L. All Rights Reserved.
13 //
14 // Redistribution and use in both source and binary forms, with or without
15 // modification, is permitted provided that the following conditions are met:
16 //
17 // 1. All redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // 2. All redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the distribution.
23 //
24 // 3. Neither the names "PixInsight" and "Pleiades Astrophoto", nor the names
25 // of their contributors, may be used to endorse or promote products derived
26 // from this software without specific prior written permission. For written
27 // permission, please contact info@pixinsight.com.
28 //
29 // 4. All products derived from this software, in any form whatsoever, must
30 // reproduce the following acknowledgment in the end-user documentation
31 // and/or other materials provided with the product:
32 //
33 // "This product is based on software from the PixInsight project, developed
34 // by Pleiades Astrophoto and its contributors (https://pixinsight.com/)."
35 //
36 // Alternatively, if that is where third-party acknowledgments normally
37 // appear, this acknowledgment must be reproduced in the product itself.
38 //
39 // THIS SOFTWARE IS PROVIDED BY PLEIADES ASTROPHOTO AND ITS CONTRIBUTORS
40 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
41 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PLEIADES ASTROPHOTO OR ITS
43 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
44 // EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, BUSINESS
45 // INTERRUPTION; PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; AND LOSS OF USE,
46 // DATA OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 // POSSIBILITY OF SUCH DAMAGE.
50 // ----------------------------------------------------------------------------
51 
52 #ifndef __PCL_Arguments_h
53 #define __PCL_Arguments_h
54 
56 
57 #include <pcl/Defs.h>
58 
59 #include <pcl/Flags.h>
60 #include <pcl/String.h>
61 #include <pcl/StringList.h>
62 
63 namespace pcl
64 {
65 
66 // ----------------------------------------------------------------------------
67 
72 // ----------------------------------------------------------------------------
73 
195 class PCL_CLASS Argument
196 {
197 public:
198 
203  {
204  Initialize();
205  }
206 
210  Argument( const String& argv )
211  {
212  Parse( argv.c_str() );
213  }
214 
218  Argument( const char16_type* argv )
219  {
220  Parse( argv );
221  }
222 
228  Argument( const String& argv, const StringList& items )
229  {
230  type = item_arg;
231  token = argv;
232  asItems = items;
233  }
234 
238  Argument( const Argument& ) = default;
239 
243  Argument& operator =( const Argument& ) = default;
244 
249  bool operator ==( const Argument& x ) const
250  {
251  if ( id == x.id && type == x.type )
252  {
253  if ( IsLiteral() || !IsValid() )
254  return true;
255  if ( IsNumeric() )
256  return asNumeric == x.asNumeric;
257  if ( IsString() )
258  return asString == x.asString;
259  if ( IsSwitch() )
260  return asSwitch == x.asSwitch;
261  if ( IsItemList() )
262  return asItems == x.asItems;
263  }
264  return false;
265  }
266 
270  bool IsValid() const
271  {
272  return type != invalid_arg;
273  }
274 
280  String Token() const
281  {
282  return token;
283  }
284 
290  String Id() const
291  {
292  return id;
293  }
294 
300  bool IsItemList() const
301  {
302  return type == item_arg;
303  }
304 
311  const StringList& Items() const
312  {
313  return asItems;
314  }
315 
323  {
324  return asItems;
325  }
326 
331  bool IsLiteral() const
332  {
333  return type == literal_arg;
334  }
335 
340  bool IsSwitch() const
341  {
342  return type == switch_arg;
343  }
344 
349  bool SwitchState() const
350  {
351  return asSwitch;
352  }
353 
358  bool IsNumeric() const
359  {
360  return type == numeric_arg;
361  }
362 
367  double NumericValue() const
368  {
369  return asNumeric;
370  }
371 
376  bool IsString() const
377  {
378  return type == string_arg;
379  }
380 
386  {
387  return asString;
388  }
389 
390 private:
391 
392  enum arg_type { invalid_arg = -1, item_arg, literal_arg, switch_arg, numeric_arg, string_arg };
393 
394  String token; // argument token
395  String id; // argument id
396  arg_type type; // argument type, or invalid
397 
398  StringList asItems; // list of non-parameters (e.g. file names or view ids)
399  bool asSwitch; // logical state of a switch argument
400  double asNumeric; // value of a numerical argument
401  String asString; // value of a string argument
402 
403  void Initialize()
404  {
405  token.Clear();
406  id.Clear();
407  type = invalid_arg;
408  asItems.Clear();
409  asSwitch = false;
410  asNumeric = 0;
411  asString.Clear();
412  }
413 
414  void Parse( const char16_type* );
415 };
416 
422 using ArgumentList = Array<Argument>;
423 
424 // ----------------------------------------------------------------------------
425 
439 namespace ArgumentItemMode
440 {
441  enum value_type
442  {
443  Ignore, // Consider non-parametric items as literal items (plain strings)
444  AsFiles, // Non-parametric items are file paths
445  AsViews, // Non-parametric items are view identifiers
446  NoItems, // Non-parametric items are not allowed
447  };
448 }
449 
455 using argument_item_mode = ArgumentItemMode::value_type;
456 
457 // ----------------------------------------------------------------------------
458 
473 namespace ArgumentOption
474 {
475  enum mask_type
476  {
477  AllowWildcards = 0x00000001, // Allow wildcard characters (*?) in non-parametric items
478  NoPreviews = 0x00000002, // Don't allow preview specifications (with the '->' standard separator)
479  RecursiveDirSearch = 0x00000004, // Perform recursive directory searches for wild path specifications
480  RecursiveSearchArgs = 0x00000008, // Use standard arguments to toggle recursive directory searching
481  };
482 }
483 
489 using ArgumentOptions = Flags<ArgumentOption::mask_type>;
490 
491 // ----------------------------------------------------------------------------
492 
493 // The implementation of ExtractArguments() is slightly different in core.
494 #ifdef __PCL_BUILDING_PIXINSIGHT_APPLICATION
495 } // pcl
496 using namespace pcl;
497 namespace pi
498 {
499 #endif
500 
610  argument_item_mode mode = ArgumentItemMode::Ignore,
611  ArgumentOptions options = ArgumentOptions() );
612 
613 #ifdef __PCL_BUILDING_PIXINSIGHT_APPLICATION
614 } // pi
615 namespace pcl
616 {
617 #endif
618 
627 
638 void PCL_FUNC SetRecursiveDirSearchArgument( const String& id );
639 
649 StringList PCL_FUNC SearchDirectory( const String& filePath, bool recursive = false );
650 
673 
674 // ----------------------------------------------------------------------------
675 
676 } // pcl
677 
678 #endif // __PCL_Arguments_h
679 
680 // ----------------------------------------------------------------------------
681 // EOF pcl/Arguments.h - Released 2024-06-18T15:48:54Z
A dynamic array of command-line arguments.
A combination of ArgumentOption flags.
A command-line argument.
Definition: Arguments.h:196
const StringList & Items() const
Definition: Arguments.h:311
bool IsLiteral() const
Definition: Arguments.h:331
String Id() const
Definition: Arguments.h:290
Argument(const char16_type *argv)
Definition: Arguments.h:218
Argument(const String &argv, const StringList &items)
Definition: Arguments.h:228
String StringValue() const
Definition: Arguments.h:385
bool IsValid() const
Definition: Arguments.h:270
bool IsSwitch() const
Definition: Arguments.h:340
StringList & Items()
Definition: Arguments.h:322
double NumericValue() const
Definition: Arguments.h:367
Argument(const Argument &)=default
bool IsNumeric() const
Definition: Arguments.h:358
Argument(const String &argv)
Definition: Arguments.h:210
bool SwitchState() const
Definition: Arguments.h:349
bool IsString() const
Definition: Arguments.h:376
bool IsItemList() const
Definition: Arguments.h:300
String Token() const
Definition: Arguments.h:280
A type-safe collection of enumerated flags.
Definition: Flags.h:85
const_c_string c_str() const noexcept
Definition: String.h:1150
Dynamic list of Unicode (UTF-16) strings.
Unicode (UTF-16) string.
Definition: String.h:8113
Represents an ArgumentItemMode enumerated value.
String PCL_FUNC ReplaceEnvironmentVariables(const String &s)
void PCL_FUNC SetRecursiveDirSearchArgument(const String &id)
ArgumentList PCL_FUNC ExtractArguments(const StringList &argv, argument_item_mode mode=ArgumentItemMode::Ignore, ArgumentOptions options=ArgumentOptions())
String PCL_FUNC RecursiveDirSearchArgument()
StringList PCL_FUNC SearchDirectory(const String &filePath, bool recursive=false)
bool operator==(const Array< T, A > &x1, const Array< T, A > &x2) noexcept
Definition: Array.h:2267
uint16 char16_type
Definition: Defs.h:1141
PCL root namespace.
Definition: AbstractImage.h:77