EnumGen

EnumGen is a library and CLI tool that makes it easy to create enums from strings.

Usage

Library

Example

let generator = try EnumGen(strings: Locale.isoLanguageCodes, enumName: "LanguageCode", enumType: "String")
try generator.generate()

Result

// This is a generated file.
// Generated by EnumGen, see https://github.com/Ryu0118/EnumGen

import Foundation

public enum LanguageCode: String {
    case aa
    case ab
    case ae
    case af
    case ak
    case am
    .......
}

Arguments

Argument Type Description
strings String Array of String types to be converted to enum
enumName String Enum type name
enumType String? Types that enum inherits
path String Absolute path to generate enum file. Default value is set to #path

CLI Tool

USAGE: enumgen [--separator <separator>] [--enum-name <enum-name>] [--delimiter <delimiter>] [--raw-value] [--enum-type <enum-type>] <path>

ARGUMENTS:
  <path>                  The path (relative or absolute) of the file you want to convert to enum

OPTIONS:
  -s, --separator <separator>
                          Separator to convert text files to enum cases (default: 
                          )
  -e, --enum-name <enum-name>
                          Name of enum type
  -d, --delimiter <delimiter>
                          If you want to recognize invalid_name as a case of enum, you can convert
                          it to lower camel case by setting the value of the —delimiter option to
                          "_" (like case invalidName)
  --raw-value             Assign a value to each element
  --enum-type <enum-type> Make enum inherit the specified type. Only integer literals, floating
                          point numeric literals, and string literals can be inherited (default:
                          String)
  -h, --help              Show help information.

Example1

To change a txt file that lists the names of SFSymbols to enum,

square.and.arrow.up
square.and.arrow.up.fill
square.and.arrow.up.circle
square.and.arrow.up.circle.fill
square.and.arrow.up.trianglebadge.exclamationmark
square.and.arrow.down
square.and.arrow.down.fill
square.and.arrow.up.on.square
square.and.arrow.up.on.square.fill
square.and.arrow.down.on.square
square.and.arrow.down.on.square.fill
rectangle.portrait.and.arrow.right
rectangle.portrait.and.arrow.right.fill
......

then run this.

$ enumgen sfsymbols.txt --raw-value --enum-name SFSymbols --delimiter .

Result

// This is a generated file.
// Generated by EnumGen, see https://github.com/Ryu0118/EnumGen

import Foundation

enum SFSymbols: String {
    case squareAndArrowUp = "square.and.arrow.up"
    case squareAndArrowUpFill = "square.and.arrow.up.fill"
    case squareAndArrowUpCircle = "square.and.arrow.up.circle"
    case squareAndArrowUpCircleFill = "square.and.arrow.up.circle.fill"
    case squareAndArrowUpTrianglebadgeExclamationmark = "square.and.arrow.up.trianglebadge.exclamationmark"
    case squareAndArrowDown = "square.and.arrow.down"
    case squareAndArrowDownFill = "square.and.arrow.down.fill"
    case squareAndArrowUpOnSquare = "square.and.arrow.up.on.square"
    case squareAndArrowUpOnSquareFill = "square.and.arrow.up.on.square.fill"
    case squareAndArrowDownOnSquare = "square.and.arrow.down.on.square"
    case squareAndArrowDownOnSquareFill = "square.and.arrow.down.on.square.fill"
    case rectanglePortraitAndArrowRight = "rectangle.portrait.and.arrow.right"
    case rectanglePortraitAndArrowRightFill = "rectangle.portrait.and.arrow.right.fill"
    ......
}

Example2

test.txt

invalid_name,invalid_file,invalid_url
$ enumgen test.txt --raw-value --enum-name Test --separator , --delimiter _ 

Result

// This is a generated file.
// Generated by EnumGen, see https://github.com/Ryu0118/EnumGen

import Foundation

enum Test: String {
    case invalidName = "invalid_name"
    case invalidFile = "invalid_file"
    case invalidUrl = "invalid_url"
}

Example3

color.txt

red
green
blue
$ enumgen color.txt --enum-name Colors

Result

// This is a generated file.
// Generated by EnumGen, see https://github.com/Ryu0118/EnumGen

import Foundation

enum Colors {
    case red
    case green
    case blue
}

GitHub

View Github