Pub Lish Executable Command Line Application with Dart

onat çipli
Flutter Community
Published in
4 min readDec 3, 2019

Today I will be creating an executable command-line application and publish that package in pub.dev. The package will be about creating files (.dart ) inside the ./lib directory.

Inspired from the Ionic generate commands, In ionic, you can generate pages, components, providers via command-line. So I want to create it for flutter too.

Step 1 (Creating the command-line application)

Create it from the command line, stagehand is a Dart command-line application which gives you templates for creating Dart apps

// first of all to use the package we need to activate it with
// following command
$ pub global activate stagehand// then We can create the command-line application$ mkdir your_project_name
$ cd your_project_name
$ stagehand console-full

or Alternatively creat from your Android Studio or IntelliJ Idea

Step 2

Let’s make our executable, to make our command-line application executable we need to update our pubspec.yaml` file

I update the version, homepage, and author to make this is a package later.

Also, add the args package to be able to parse the commands which coming from the command-line

The names you type under the executables must match the file names in the ./bin / directory

For example, The executable that we add in pubspec.yaml file flutter_generate match with ./bin/flutter_generate.dart file.

So you can rename your /bin/main.dart file

First, we create ArgParser to parse the commands, options, and flags. I add an option(“name”) it just prints the given name console. To test it, We need to activate our command-line application first (Make sure you configured your path)

// if you are in your project root dir
pub global activate --source path .
// or just give the project path
pub global activate --source path ./path/to/your/projectDir

after that, we can run our first executable with the following code

pub global run flutter_generate --name YourNameorflutter_generate --name "make sure you configure the path"

To publish your command-line application as a package to pub.dev. You need to add LICENSE and example project which shows how to use your package. You can read the following article to learn how to publish your package.

TLDR: After adding LICENSE and example project you ready the publish your package with the pub lish command which is a shortcut for pub publish

After publishing your package you can activate your command-line app with

pub global activate your_app_name// my command-line app name is :flutter_generate so : pub global activate flutter_generate

What flutter_generate package does

flutter_generate is a simple executable command-line application that helps you to create a consistent structure in your project lib directory It is creating dart files with command-line commands and It is compatible with the effective dart style guide. For Example following commands creates these files

flutter_generate page home --stful
flutter_generate widget --name loginCard --stless
flutter_generate page --name login --stlessflutter_generate page --name signUp --stfulflutter_generate repository --name Authentication

flutter_generate model --name UserModel
flutter_generate widget --name signUpCard --stful

flutter_generate home page and also check my other package rename which renames the app name with rename --appname yourappname

You can reach and follow me on twitter @CipliOnat or Linkedin.

--

--

onat çipli
Flutter Community

Software Engineer who enjoys creating mobile applications with Flutter and Dart