This repository has been archived by the owner on Jul 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate
158 lines (130 loc) · 3.32 KB
/
generate
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env php
<?php
/**
* Generate script.
*
* Generates file with randomized floating numbers.
*
* @author Adam "Saibamen" Stachowicz <[email protected]>
* @license MIT
*
* @link https://github.com/Saibamen/Generate-Sort-Numbers
*/
namespace Generate;
use Includes\Generate;
use Includes\Input;
use Includes\Text;
// Run only in CLI
if (php_sapi_name() !== 'cli') {
exit('This is console app. Please run it in console (CLI)!');
}
require_once 'Includes/Generate.php';
require_once 'Includes/Input.php';
require_once 'Includes/File.php';
// Console arguments
$longOpts = array(
'min:',
'max:',
'decimal:',
'size:',
'output:',
'ext:',
'delimiter:',
'debug',
'testing',
);
$args = getopt(null, $longOpts);
// Extension
$fileExtension = '.dat';
if (isset($args['ext'])) {
// preg match for \ and /
if (Input::isFilenameWrong($args['ext']) || preg_match('/[\/\\\\]/', $args['ext'])) {
Text::message('Wrong file extension.');
} else {
$fileExtension = $args['ext'];
}
}
// Delimiter
$delimiter = ' ';
if (isset($args['delimiter'])) {
if (preg_match('/[0-9-.]/', $args['delimiter'])) {
Text::message('Wrong delimiter.');
} else {
$delimiter = $args['delimiter'];
}
}
// Debug mode
if (isset($args['debug'])) {
Text::message('Debug mode. Have fun!');
Text::setDebug(true);
}
// Testing mode
if (isset($args['testing'])) {
Text::message('Testing mode');
Generate::setTesting(true);
}
// Min
if (isset($args['min'])) {
if (!is_numeric($args['min'])) {
Text::message('Wrong minimum allowed number.');
} else {
$min = (float) $args['min'];
}
}
// Max
if (isset($args['max'])) {
if (!is_numeric($args['max'])) {
Text::message('Wrong maximum allowed number.');
} else {
$max = (float) $args['max'];
}
}
// Decimal places
if (isset($args['decimal'])) {
if (!is_numeric($args['decimal'])) {
Text::message('Wrong decimal places number.');
} else {
$decimalPlaces = (float) $args['decimal'];
}
}
// File size
if (isset($args['size'])) {
$checkInput = Input::getBytesFromString($args['size']);
if ($checkInput['error']) {
Text::message('Wrong file size.');
} else {
$fileSize = $checkInput['bytes'];
}
unset($checkInput);
}
// Output
if (isset($args['output'])) {
if (Input::isFilenameWrong($args['output'])) {
Text::message('Wrong output filename.');
} else {
$filename = $args['output'];
}
}
unset($args);
// Collect User input
if (!isset($min)) {
$min = Input::getNumber('Type minimum allowed number');
}
if (!isset($max)) {
$max = Input::getNumber('Type maximum allowed number', 10);
}
// Switch min and max if min > max
$minMax = Generate::checkSwitchMinMax($min, $max);
$min = $minMax['min'];
$max = $minMax['max'];
if (!isset($decimalPlaces)) {
$decimalPlaces = Input::getNumber('Type number of decimal places');
}
if (!isset($fileSize)) {
$fileSize = Input::getFileSize("Type maximum file size\neg. `5B`, `5KB`, `5MB`, `5GB`");
}
if (!isset($filename)) {
$filename = Input::getFilename('Type output filename (without extension)');
}
// Generate and save to file
Generate::generateAndSaveRandomNumbers($min, $max, $decimalPlaces, $fileSize, $filename, $fileExtension, $delimiter);