forked from jhthorsen/applify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
209 lines (146 loc) · 5.67 KB
/
README
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
NAME
Applify - Write object oriented scripts with ease
VERSION
0.08
DESCRIPTION
This module should keep all the noise away and let you write scripts
very easily. These scripts can even be unittested even though they are
define directly in the script file and not in a module.
SYNOPSIS
#!/usr/bin/perl
use Applify;
option file => input_file => 'File to read from';
option dir => output_dir => 'Directory to write files to';
option flag => dry_run => 'Use --no-dry-run to actually do something', 1;
documentation __FILE__;
version 1.23;
sub generate_exit_value {
return int rand 100;
}
app {
my($self, @extra) = @_;
my $exit_value = 0;
print "Extra arguments: @extra\n" if(@extra);
print "Will read from: ", $self->input_file, "\n";
print "Will write files to: ", $self->output_dir, "\n";
if($self->dry_run) {
die 'Will not run script';
}
return $self->generate_exit_value;
};
APPLICATION CLASS
This module will generate an application class, which $self inside the
"app" block refere to. This class will have:
* new()
An object constructor. This method will not be auto generated if any
of the classes given to "extends" has the method "new()".
* run()
This method is basically the code block given to "app".
* Other methods
Other methods defined in the script file will be accesible from
$self inside "app{}".
* _script()
This is an accessor which return the Applify object which is refered
to as $self in this documentation.
NOTE: This accessor starts with an underscore to prevent conflicts
with "options".
* Other accessors
Any "option" (application switch) will be available as an accessor
on the application object.
EXPORTED FUNCTIONS
option
option $type => $name => $documentation;
option $type => $name => $documentation, $default;
option $type => $name => $documentation, $default, @args;
option $type => $name => $documentation, @args;
This function is used to define options which can be given to this
application. See "SYNOPSIS" for example code. This function can also be
called as a method on $self.
* $type
Used to define value types for this input.
bool, flag
inc
str
int
num
file (TODO)
dir (TODO)
* $name
The name of an application switch. This name will also be used as
accessor name inside the application. Example:
# define an application switch:
option file => some_file => '...';
# call the application from command line:
> myapp.pl --some-file /foo/bar
# run the application code:
app {
my $self = shift;
print $self->some_file # prints "/foo/bar"
return 0;
};
* $documentation
Used as description text when printing the usage text.
* @args
* "required"
The script will not start if a required field is omitted.
* "n_of"
Allow the option to hold a list of values. Examples: "@", "4",
"1,3". See "Options-with-multiple-values" in Getopt::Long for
details.
* Other
Any other Moose attribute argument may/will be supported in
future release.
documentation
documentation __FILE__; # current file
documentation '/path/to/file';
documentation 'Some::Module';
Specifies where to retrieve documentaion from when giving the "--man"
switch to your script.
version
version 'Some::Module';
version $num;
Specifies where to retrieve the version number from when giving the
"--version" switch to your script.
extends
extends @classes;
Specify which classes this application should inherit from. These
classes can be Moose based.
app
app CODE;
This function will define the code block which is called when the
application is started. See "SYNOPSIS" for example code. This function
can also be called as a method on $self.
IMPORTANT: This function must be the last function called in the script
file for unittests to work. Reason for this is that this function runs
the application in void context (started from command line), but returns
the application object in list/scalar context (from "do" in perlfunc).
ATTRIBUTES
options
$array_ref = $self->options;
Holds the application options given to "option".
METHODS
new
$self = $class->new({ options => $array_ref, ... });
Object constructor. Creates a new object representing the script meta
information.
print_help
Will print "options" to selected filehandle (STDOUT by default) in a
normalized matter. Example:
Usage:
--foo Foo does this and that
* --bar Bar does something else
--help Print this help text
--man Display manual for this application
--version Print application name and version
print_version
Will print "version" to selected filehandle (STDOUT by default) in a
normalized matter. Example:
some-script.pl version 1.23
import
Will export the functions listed under "EXPORTED FUNCTIONS". The
functions will act on a Applify object created by this method.
COPYRIGHT & LICENSE
This library is free software. You can redistribute it and/or modify it
under the same terms as Perl itself.
AUTHOR
Jan Henning Thorsen