Subversion Repositories spk

Rev

Rev 303 | Rev 319 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
303 cycrow 1
#pragma once
2
 
3
#include "WString.h"
4
#include "WStringList.h"
5
#include "../File_IO.h"
6
#include "../DirIO.h"
7
 
8
namespace Utils
9
{
10
	class CommandLine
11
	{
12
	private:
13
		std::shared_ptr<CFileIO> _file;
14
		Utils::WStringList _options;
15
		std::vector<Utils::WString> _args;
16
 
17
	public:
18
		CommandLine(int argc, char* argv[])
19
		{
20
			_file = std::make_shared<CFileIO>(argv[0]);
21
 
22
			for (int i = 1; i < argc; i++)
23
			{
24
				Utils::WString arg(argv[i]);
25
				if (arg.startsWith(L"--"))
26
				{
27
					arg = arg.substr(2);
28
 
29
					if (arg.contains(L":"))
30
					{
31
						Utils::WString a = arg.token(L":", 1);
32
						Utils::WString b = arg.tokens(L":", 2);
33
						_options.pushBack(a, b);
34
					}
35
					else
36
						_options.pushBack(arg);
37
				}
38
				else
39
					_args.push_back(arg);
40
			}
309 cycrow 41
 
42
			if (_file->dir().empty() || !_file->dir().contains("/"))
43
			{
44
				Utils::WString d;
45
#ifdef _WIN32
46
				d = Utils::WString(_getcwd(NULL, 0));
47
#else
48
				d = Utils::WString(getcwd(NULL, 0));
49
#endif
50
				if (d.empty())
51
					d = L"./";
52
				_file->setDir(d);
53
			}
303 cycrow 54
		}
55
 
56
		const std::shared_ptr<CFileIO> file() const
57
		{
58
			return _file;
59
		}
60
 
61
		const Utils::WString& cmdName() const
62
		{
63
			return _file->filename();
64
		}
65
 
66
		const Utils::WString& cmdDir() const
67
		{
68
			return _file->dir();
69
		}
70
 
71
		const Utils::WStringList options() const
72
		{
73
			return _options;
74
		}
75
 
76
		const std::vector<Utils::WString>& args() const
77
		{
78
			return _args;
79
		}
80
 
81
		size_t argCount() const
82
		{
83
			return _args.size();
84
		}
85
 
86
		Utils::WString arg(size_t i) const
87
		{
88
			if(i >= _args.size())
89
				return Utils::WString::Null();
90
			return _args[i];
91
		}
92
 
93
		bool hasSwitch(const Utils::WString& s) const
94
		{
95
			return _options.contains(s);
96
		}
97
 
98
		Utils::WString switchData(const Utils::WString& s) const
99
		{
100
			if (_options.contains(s))
101
				return _options[s]->data;
102
			return Utils::WString::Null();
103
		}
104
	};
105
}