{"id":17306460,"url":"https://github.com/jhthorsen/applify","last_synced_at":"2025-04-14T13:35:00.572Z","repository":{"id":2498779,"uuid":"3473278","full_name":"jhthorsen/applify","owner":"jhthorsen","description":"Write object oriented scripts with ease","archived":false,"fork":false,"pushed_at":"2022-04-21T03:32:31.000Z","size":155,"stargazers_count":6,"open_issues_count":1,"forks_count":5,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-28T02:45:46.210Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://metacpan.org/dist/Applify","language":"Perl","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jhthorsen.png","metadata":{"files":{"readme":"README.md","changelog":"Changes","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-02-17T19:49:26.000Z","updated_at":"2022-08-20T13:35:08.000Z","dependencies_parsed_at":"2022-09-09T07:50:35.339Z","dependency_job_id":null,"html_url":"https://github.com/jhthorsen/applify","commit_stats":null,"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhthorsen%2Fapplify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhthorsen%2Fapplify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhthorsen%2Fapplify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jhthorsen%2Fapplify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jhthorsen","download_url":"https://codeload.github.com/jhthorsen/applify/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248889058,"owners_count":21178156,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T11:58:27.838Z","updated_at":"2025-04-14T13:35:00.545Z","avatar_url":"https://github.com/jhthorsen.png","language":"Perl","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NAME\n\nApplify - Write object oriented scripts with ease\n\n# VERSION\n\n0.23\n\n# DESCRIPTION\n\nThis module should keep all the noise away and let you write scripts very\neasily. These scripts can even be unit tested even though they are defined\ndirectly in the script file and not in a module.\n\n# SYNOPSIS\n\n    #!/usr/bin/perl\n    use Applify;\n\n    option file =\u003e input_file =\u003e 'File to read from';\n    option dir =\u003e output_dir =\u003e 'Directory to write files to';\n    option flag =\u003e dry_run =\u003e 'Use --no-dry-run to actually do something', 1;\n\n    documentation __FILE__;\n    version 1.23;\n\n    sub generate_exit_value {\n      return int rand 100;\n    }\n\n    # app {...}; must be the last statement in the script\n    app {\n      my ($app, @extra) = @_;\n      my $exit_value = 0;\n\n      print \"Extra arguments: @extra\\n\" if(@extra);\n      print \"Will read from: \", $app-\u003einput_file, \"\\n\";\n      print \"Will write files to: \", $app-\u003eoutput_dir, \"\\n\";\n\n      if($app-\u003edry_run) {\n        die 'Will not run script';\n      }\n\n      return $app-\u003egenerate_exit_value;\n    };\n\n# APPLICATION CLASS\n\nThis module will generate an application class, which `$app` inside the\n[\"app\"](#app) block is an instance of. The class will have these methods:\n\n- `new()`\n\n    An object constructor. This method will not be auto generated if any of\n    the classes given to [\"extends\"](#extends) has the method `new()`.\n\n- `run()`\n\n    This method is basically the code block given to [\"app\"](#app).\n\n- Other methods\n\n    Other methods defined in the script file will be accesible from `$app`\n    inside `app{}`.\n\n- `_script()`\n\n    This is an accessor which return the [Applify](https://metacpan.org/pod/Applify) object which\n    is refered to as `$script` in this documentation.\n\n    NOTE: This accessor starts with an underscore to prevent conflicts\n    with [\"options\"](#options).\n\n- Other accessors\n\n    Any [\"option\"](#option) (application option) will be available as an accessor on the\n    application object.\n\n# EXPORTED FUNCTIONS\n\n## option\n\n    option $type =\u003e $name =\u003e $documentation;\n    option $type =\u003e $name =\u003e $documentation, $default;\n    option $type =\u003e $name =\u003e $documentation, $default, @args;\n    option $type =\u003e $name =\u003e $documentation, @args;\n\nThis function is used to define options which can be given to this\napplication. See [\"SYNOPSIS\"](#synopsis) for example code. This function can also be\ncalled as a method on `$script`. Additionally, similar to\n[Moose attributes](https://metacpan.org/pod/Moose%3A%3AManual%3A%3AAttributes%23Predicate-and-clearer-methods), a\n`has_$name` method will be generated, which can be called on `$app` to\ndetermine if the [\"option\"](#option) has been set, either by a user or from the\n`$default`.\n\n- `$type`\n\n    Used to define value types for this input. Can be:\n\n        | $type | Example             | Attribute value |\n        |-------|---------------------|-----------------|\n        | bool  | --foo, --no-foo     | foo=1, foo=0    |\n        | flag  | --foo, --no-foo     | foo=1, foo=0    |\n        | inc   | --verbose --verbose | verbose=2       |\n        | str   | --name batwoman     | name=batwoman   |\n        | int   | --answer 42         | answer=42       |\n        | num   | --pie 3.14          | pie=3.14        |\n\n- `$name`\n\n    The name of an application option. This name will also be used as accessor name\n    inside the application. Example:\n\n        # define an application option: \n        option file =\u003e some_file =\u003e '...';\n\n        # call the application from command line:\n        \u003e myapp.pl --some-file /foo/bar\n\n        # run the application code:\n        app {\n          my $app = shift;\n          print $app-\u003esome_file # prints \"/foo/bar\"\n          return 0;\n        };\n\n- `$documentation`\n\n    Used as description text when printing the usage text.\n\n- `$default`\n\n    Either a plain value or a code ref that can be used to generate a value.\n\n        option str =\u003e passwd =\u003e \"Password file\", \"/etc/passwd\";\n        option str =\u003e passwd =\u003e \"Password file\", sub { \"/etc/passwd\" };\n\n- `@args`\n    - `alias`\n\n        Used to define an alias for the option. Example:\n\n            option inc =\u003e verbose =\u003e \"Output debug information\", alias =\u003e \"v\";\n\n    - `required`\n\n        The script will not start if a required field is omitted.\n\n    - `n_of`\n\n        Allow the option to hold a list of values. Examples: \"@\", \"4\", \"1,3\".\n        See [\"Options-with-multiple-values\" in Getopt::Long](https://metacpan.org/pod/Getopt%3A%3ALong#Options-with-multiple-values) for details.\n\n    - `isa`\n\n        Can be used to either specify a class that the value should be instantiated\n        as, or a [Type::Tiny](https://metacpan.org/pod/Type%3A%3ATiny) object that will be used for coercion and/or type\n        validation.\n\n        Example using a class:\n\n            option file =\u003e output =\u003e \"output file\", isa =\u003e \"Mojo::File\";\n\n        The `output()` attribute will then later return an object of [Mojo::File](https://metacpan.org/pod/Mojo%3A%3AFile),\n        instead of just a plain string.\n\n        Example using [Type::Tiny](https://metacpan.org/pod/Type%3A%3ATiny):\n\n            use Types::Standard \"Int\";\n            option num =\u003e age =\u003e \"Your age\", isa =\u003e Int;\n\n    - Other\n\n        Any other [Moose](https://metacpan.org/pod/Moose) attribute argument may/will be supported in\n        future release.\n\n## documentation\n\n    documentation __FILE__; # current file\n    documentation '/path/to/file';\n    documentation 'Some::Module';\n\nSpecifies where to retrieve documentaion from when giving the `--man` option\nto your script.\n\n## version\n\n    version 'Some::Module';\n    version $num;\n\nSpecifies where to retrieve the version number from when giving the\n`--version` option to your script.\n\n## extends\n\n    extends @classes;\n\nSpecify which classes this application should inherit from. These\nclasses can be [Moose](https://metacpan.org/pod/Moose) based.\n\n## hook\n\n    hook before_exit            =\u003e sub { my ($script, $exit_value) = @_ };\n    hook before_options_parsing =\u003e sub { my ($script, $argv) = @_ };\n\nDefines a hook to run.\n\n- before\\_exit\n\n    Called right before `exit($exit_value)` is called by [Applify](https://metacpan.org/pod/Applify). Note that\n    this hook will not be called if an exception is thrown.\n\n- before\\_options\\_parsing\n\n    Called right before `$argv` is parsed by [\"option\\_parser\"](#option_parser). `$argv` is an\n    array-ref of the raw options given to your application. This hook allows you\n    to modify [\"option\\_parser\"](#option_parser). Example:\n\n        hook before_options_parsing =\u003e sub {\n          shift-\u003eoption_parser-\u003econfigure(bundling no_pass_through);\n        };\n\n## subcommand\n\n    subcommand list =\u003e 'provide a listing objects' =\u003e sub {\n      option flag =\u003e long =\u003e 'long listing';\n      option flag =\u003e recursive =\u003e 'recursively list objects';\n    };\n\n    subcommand create =\u003e 'create a new object' =\u003e sub {\n      option str =\u003e name =\u003e 'name of new object', required =\u003e 1;\n      option str =\u003e description =\u003e 'description for the object', required =\u003e 1;\n    };\n\n    sub command_create {\n      my ($app, @extra) = @_;\n      ## do creating\n      return 0;\n    }\n\n    sub command_list {\n      my ($app, @extra) = @_;\n      ## do listing\n      return 0;\n    }\n\n    app {\n      my ($app, @extra) = @_;\n      ## fallback when no command given.\n      $app-\u003e_script-\u003eprint_help;\n      return 0;\n    };\n\nThis function allows for creating multiple related sub commands within the same\nscript in a similar fashion to `git`. The [\"option\"](#option), [\"extends\"](#extends) and\n[\"documentation\"](#documentation) exported functions may sensibly be called within the\nsubroutine. Calling the function with no arguments will return the running\nsubcommand, i.e. a valid `$ARGV[0]`. Non valid values for the subcommand given\non the command line will result in the help being displayed.\n\n## app\n\n    app CODE;\n\nThis function will define the code block which is called when the application\nis started. See [\"SYNOPSIS\"](#synopsis) for example code. This function can also be\ncalled as a method on `$script`.\n\nIMPORTANT: This function must be the last function called in the script file\nfor unit tests to work. Reason for this is that this function runs the\napplication in void context (started from command line), but returns the\napplication object in list/scalar context (from [\"do\" in perlfunc](https://metacpan.org/pod/perlfunc#do)).\n\n# ATTRIBUTES\n\n## option\\_parser\n\n    $script = $script-\u003eoption_parser(Getopt::Long::Parser-\u003enew);\n    $parser = $script-\u003eoption_parser;\n\nYou can specify your own option parser if you have special needs. The default\nis:\n\n    Getopt::Long::Parser-\u003enew(config =\u003e [qw(no_auto_help no_auto_version pass_through)]);\n\n## options\n\n    $array_ref = $script-\u003eoptions;\n\nHolds the application options given to [\"option\"](#option).\n\n# METHODS\n\n## new\n\n    $script = Applify-\u003enew({options =\u003e $array_ref, ...});\n\nObject constructor. Creates a new object representing the script meta\ninformation.\n\n## print\\_help\n\nWill print [\"options\"](#options) to selected filehandle (STDOUT by default) in\na normalized matter. Example:\n\n    Usage:\n       --foo      Foo does this and that\n     * --bar      Bar does something else\n\n       --help     Print this help text\n       --man      Display manual for this application\n       --version  Print application name and version\n\n## print\\_version\n\nWill print [\"version\"](#version) to selected filehandle (STDOUT by default) in\na normalized matter. Example:\n\n    some-script.pl version 1.23\n\n## import\n\nWill export the functions listed under [\"EXPORTED FUNCTIONS\"](#exported-functions). The functions\nwill act on a [Applify](https://metacpan.org/pod/Applify) object created by this method.\n\n# COPYRIGHT \u0026 LICENSE\n\nThis library is free software. You can redistribute it and/or modify\nit under the same terms as Perl itself.\n\n# AUTHORS\n\nJan Henning Thorsen - `jhthorsen@cpan.org`\n\nRoy Storey - `kiwiroy@cpan.org`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjhthorsen%2Fapplify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjhthorsen%2Fapplify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjhthorsen%2Fapplify/lists"}