Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): add more docs for cli annotations #157

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
257 changes: 257 additions & 0 deletions docs/cli-application/annotations.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
---
title: Annotations
sidebar_position: 5
toc_max_heading_level: 2
description: Check all available Artisan annotations and it options.
---

# Annotations

## `@Argument()`

Define an argument for an Artisan command class:

```typescript
import { Argument, BaseCommand } from '@athenna/artisan'

export class Greet extends BaseCommand {
@Argument()
public name: string

public static signature(): string {
return 'greet'
}

public async handle(): Promise<void> {
this.logger.simple(`Hello ${this.name}!`)
}
}
```

You can also define any of the following optional properties:

### `signature`

> Default: the name of your class property (e.g `name`)

The signature defines how the argument will be displayed in `help`
commands of Artisan and also how it's going to be parsed:

```typescript
@Argument({ signature: 'myName' })
public name: string
```

By default the signature of an argument will be set as required (`<myName>`),
the define that an argument is optional use the following syntax:

```typescript
@Argument({ signature: '[myName]' })
public name: string
```

:::tip

To avoid using the syntax above to define your argument as optional
or not we recommend you to use the [`required`](/docs/cli-application/annotations#required)

:::

You can also define a catch-all argument, we call it spread/variadic
arguments. It works exactly like rest parameters of arrays in JavaScript,
and it **must always be the last argument of your command class**:

```typescript
@Argument({ signature: 'myNames...' })
public myNames: string[]
```

### `default`

> Default: `undefined`

Set a default value for your argument:

```typescript
@Argument({ default: 'Lenon' })
public: name: string
```

:::note

Defining a value to `default` option automatically set the `required`
option to `false`.

:::

### `required`

> Default: `true`

Define that argument is an optional field or not:

```typescript
@Argument({ required: false })
public name?: string
```

### `description`

> Default: `undefined`

Define what description will be displayed in commands like `help` of
Artisan:

```typescript
@Argument({ description: 'The name that Athenna should greet' })
public name: string
```

## `@Option()`

Define an option for an Artisan command class:

```typescript
import { Option, BaseCommand } from '@athenna/artisan'

export class Greet extends BaseCommand {
@Option({ signature: '-n, --name <name>' })
public name: string

public static signature(): string {
return 'greet'
}

public async handle(): Promise<void> {
this.logger.simple(`Hello ${this.name}!`)
}
}
```

You can also define any of the following optional properties:

### `signature`

> Default: the name of your class property with `--` in the
> beggining (e.g `--name`)

The signature defines how the option will be displayed in `help`
commands of Artisan, how it's going to be parsed and also how
the user that will be using the CLI will set the option:

```typescript
@Option({ signature: '-n, --name' })
public name: boolean
```

Calling `greet` command setting the `name` option:

```shell
node artisan greet -n

// Or

node artisan greet --name
```

As you can see the type of the `name` property is a `boolean`. If you
want to define that your option will receive a value string you can
use the following signature:

```typescript
@Option({ signature: '-n, --name <name>' })
public name: string
```

Calling `greet` command setting the `name` option:

```shell
node artisan greet -n Lenon

// Or

node artisan greet --name Lenon
```

Sometimes you might need to add more than one `name` option. For this
kind of situation, just like arguments, you can define a spread option:

```typescript
@Option({ signature: '-n, --name <name...>' })
public names: string[]
```

Calling `greet` command setting multiple `name` options:

```typescript
node artisan greet -n Lenon -n Txsoura

// or

node artisan greet --name Lenon --name Txsoura
```

Is possible to create a signature with a negate value, check the example:

```typescript
@Option({ signature: '--no-name' })
public hasName: boolean
```

If you call `greet` command passing the `--no-name` option, the value
of the `hasName` property will be `false`.

### `default`

> Default: `undefined`

Set a default value for your option:

```typescript
@Option({
signature: '-n, --name <name>',
default: 'Lenon'
})
public: name: string
```

### `description`

> Default: `undefined`

Define what description will be displayed in commands like `help` of
Artisan:

```typescript
@Option({
signature: '-n, --name <name>'
description: 'The name that Athenna should greet'
})
public name: string
```

### `isFromGlobal`

> Default: `false`

Is possible to define global options to Artisan to be used in all
commands. To be able to catch the value of a global one, you must
define the `isFromGlobal` as true and set the exactly `signature`
that you see when running `node artisan --help`. By default, Athenna
comes with `--env` global option, let's use it as example:

```typescript
@Option({
isFromGlobal: true,
signature: '--env <name>'
})
public env: string
```

:::warning

The `default` and `description` options are ignored when `isFromGlobal`
is true, since Athenna will use the values set for the global option.

:::

24 changes: 7 additions & 17 deletions docs/cli-application/commands.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,9 @@ Will output:

#### Arguments options

The `@Argument()` annotation accept the following options:

- `signature` - The signature of the argument. If not present
the name of the class property will be used.
- `description` - The description of the argument. This will
be displayed in the help output.
- `required` - Set if the argument is required or not. This
could also be done in the `signature` using `<argName>` for
required and `[argName]` for optional.
To check all the available options for the `@Argument()`
annotation and see details arround them, check the
[`@Argument()` annotation documentation section.](/docs/cli-application/annotations#argument)

### Options

Expand Down Expand Up @@ -538,16 +532,12 @@ Will output:
[ 'lenon', 'txsoura' ]
```

#### Option options
#### Options options

The `@Option()` annotation accept the following options:
To check all the available options for the `@Option()`
annotation and see details arround them, check the
[`@Option()` annotation documentation section.](/docs/cli-application/annotations#option)

- `signature` - The signature of the option. If not present
the name of the class property will be used.
- `description` - The description of the option. This will
be displayed in the help output.
- `default` - Set the default value for the option if not
present.

## Prompts

Expand Down
Loading