Version Parsing
Setup: See /installation for one-time SDK/CLI/MCP install.
Layers: SDK (Go) → CLI (shell) → MCP (AI tools) — pick your entry point.
When to Use
- Parse a version string (
"v1.2.3-rc1") into structured components (prefix, numbers, suffix)
- Validate whether a string is a valid version number
- Determine version type: prerelease, stable, alpha, beta, RC, dev, snapshot, nightly, etc.
- Extract segments, sub-version, suffix weight, group ID, or core version
- Use custom delimiters for non-standard formats (e.g. underscore-separated)
Decision Tree
Raw version string → what do you need?
├─ Structured breakdown? → NewVersion() / version_parse / versions parse
├─ Yes/no validity? → IsValid() / version_validate / versions validate
├─ All type flags at once? → version_info / versions info
├─ Specific type check? → IsBeta(), IsStable(), IsRC() etc.
├─ Segments (major/minor/patch)? → Major()/Minor()/Patch()/Segments()
├─ Core version (no suffix)? → Core()
└─ Suffix weight for ordering? → SuffixWeight()
Task Patterns
Parse & inspect
Goal: Break "v1.2.3-rc1" into prefix "v", numbers [1,2,3], suffix "-rc1".
| Layer |
Approach |
| SDK |
v := versions.NewVersion("v1.2.3-rc1"); v.IsValid() |
| CLI |
versions parse v1.2.3-rc1 |
| MCP |
{"tool": "version_parse", "arguments": {"version_string": "v1.2.3-rc1"}} |
Validate
Goal: Confirm "1.2.3" is valid, "not-a-version" is not.
| Layer |
Approach |
| SDK |
v.Validate() != nil → invalid |
| CLI |
versions validate 1.2.3 (exit 0 = valid, exit 1 = invalid) |
| MCP |
{"tool": "version_validate", "arguments": {"version_string": "1.2.3"}} |
Check type
Goal: Determine if "1.0.0-beta2" is a beta prerelease.
| Layer |
Approach |
| SDK |
v.IsBeta(), v.IsPrerelease(), v.IsStable(), v.SubVersion() |
| CLI |
versions check --beta 1.0.0-beta2 (exit 0 = match) |
| MCP |
{"tool": "version_info", ...} returns all Is* flags |
Extract segments
Goal: Get [1, 2, 3] from "1.2.3".
| Layer |
Approach |
| SDK |
v.Major(), v.Minor(), v.Patch(), v.Segments() |
| CLI |
versions segments 1.2.3 |
| MCP |
{"tool": "version_parse", ...} → segments field |
Get core (strip suffix)
Goal: "v1.2.3-beta1" → "v1.2.3".
| Layer |
Approach |
| SDK |
v.Core().RawString() |
| CLI |
versions core v1.2.3-beta1 |
| MCP |
{"tool": "version_core", "arguments": {"version_string": "v1.2.3-beta1"}} |
Custom delimiters
Goal: Parse "curl-7_85_0" with - and _ delimiters.
| Layer |
Approach |
| SDK |
versions.NewVersionWithOption(s, versions.ParserOption{Delimiters: ".-_"}) |
| CLI |
versions parse --delimiters "_-" curl-7_85_0 |
| MCP |
{"tool": "version_parse", "arguments": {"version_string": "curl-7_85_0", "delimiters": ".-_"}} |
API Reference
SDK — Constructor Functions
versions.NewVersion(versionStr string) *Version // never nil, check IsValid()
versions.NewVersionE(versionStr string) (*Version, error) // returns error for invalid
versions.MustParse(versionStr string) *Version // panics on invalid — test data only
versions.NewVersions(strings ...string) []*Version // batch parse
versions.NewVersionWithOption(s string, opt ParserOption) *Version // custom delimiters
versions.NewVersionStringParser(s string) *VersionStringParser // low-level parser
SDK — Version Struct Fields
type Version struct {
Prefix string // e.g. "v"
VersionNumbers []int // e.g. [1, 2, 3]
Suffix string // e.g. "-rc1"
Metadata string // semver build metadata (after +)
PublicTime time.Time // optional release timestamp
Raw string // original input string
}
SDK — Type Checks
v.IsValid() bool // has VersionNumbers
v.IsStable() bool // no suffix
v.IsPrerelease() bool // has any suffix
v.IsAlpha() bool // suffix contains "alpha"
v.IsBeta() bool // suffix contains "beta"
v.IsRC() bool // suffix contains "rc"
v.IsDev() bool // suffix contains "dev"
v.IsSnapshot() bool // suffix contains "snapshot"
v.IsNightly() bool // suffix contains "nightly"
v.IsMilestone() bool // suffix contains "milestone" or "m"
v.IsFinal() bool // suffix contains "final"
v.IsGA() bool // suffix contains "ga"
v.IsPre() bool // suffix contains "-pre"
v.IsRelease() bool // suffix contains "-release"
v.IsSP() bool // suffix contains "sp"
v.IsPost() bool // suffix contains "post"
SDK — Segment Accessors
v.Major() int // VersionNumbers[0] or 0
v.Minor() int // VersionNumbers[1] or 0
v.Patch() int // VersionNumbers[2] or 0
v.Segments() []int // all VersionNumbers
v.Segments64() []int64 // as int64
v.SubVersion() int // numeric from suffix (e.g. "beta2" → 2)
v.SuffixWeight() SuffixWeight // semantic ordering weight
SDK — Core, Clone, Serialization
v.Core() *Version // strip suffix
v.Clone() *Version // deep copy
v.BuildGroupID() string // e.g. "1.2.3"
v.WithPrefix(p string) *Version // immutable — returns new Version
v.WithSuffix(s string) *Version
v.WithMajor(n int) *Version
v.WithMinor(n int) *Version
v.WithPatch(n int) *Version
v.WithNumbers(ns []int) *Version
v.WithPublicTime(t time.Time) *Version
v.WithMetadata(m string) *Version
// JSON/Text/SQL serialization via MarshalText/UnmarshalText/MarshalJSON/UnmarshalJSON/Scan/Value
SDK — Parser Options
type ParserOption struct {
Delimiters string // custom delimiter set, e.g. ".-_" (default: ".-")
}
CLI Commands
versions parse <version> # structured JSON output
versions parse --delimiters "_-" <v> # custom delimiters
versions validate <version> # exit 0 = valid, 1 = invalid
versions info <version> # all Is* flags + segments
versions segments <version> # [major, minor, patch, ...]
versions sub-version <version> # numeric suffix index
versions suffix-weight <version> # semantic weight int
versions pure-prefix <version> # prefix without trailing delimiters
versions group-id <version> # e.g. "v1.2.3-beta" → "1.2.3"
versions core <version> # strip suffix
versions clone <version> # deep copy as JSON
versions check --<type> <version> # --alpha, --beta, --rc, --stable, etc.
versions check --prerelease <v> # exit 0 if prerelease
versions check --is-valid <v> # exit 0 if valid
MCP Tools
| Tool |
Arguments |
Returns |
version_parse |
version_string, delimiters? |
prefix, numbers, suffix, metadata |
version_validate |
version_string |
{valid: bool, error: string?} |
version_info |
version_string |
all Is* flags, segments, suffix info |
version_core |
version_string |
core version string |
Cross-References
- [[version-check]] — boolean type checks, comparison predicates
- [[version-comparison]] — CompareTo, IsNewerThan, IsOlderThan
- [[version-sorting]] — sorting parsed versions
- [[version-constraints]] — constraint expression matching
- [[version-mutation]] — bumping, building, modifying versions
Important Notes
NewVersion() never returns nil — always check IsValid() for invalid input
IsValid() checks for non-empty VersionNumbers; Validate() is stricter (rejects negative numbers)
IsPrerelease() means "has any suffix"; IsPre() means "has explicit -pre suffix"
IsStable() means "no suffix"; IsRelease() means "has explicit -release suffix"
- Semver build metadata (
+ part) is in Metadata field, not Suffix
- All
With* methods return new Version objects — the original is never modified
MustParse panics on invalid input — use only for hardcoded/test data
1---2name: version-parsing3description: Parse, validate, and extract structured components from version strings via SDK, CLI, or MCP. Covers NewVersion/MustParse, all Is* type checks, Segments, Core, Clone, SuffixWeight, custom parser options.4---56# Version Parsing78> **Setup:** See `/installation` for one-time SDK/CLI/MCP install. 9> **Layers:** SDK (Go) → CLI (shell) → MCP (AI tools) — pick your entry point.1011## When to Use1213- Parse a version string (`"v1.2.3-rc1"`) into structured components (prefix, numbers, suffix)14- Validate whether a string is a valid version number15- Determine version type: prerelease, stable, alpha, beta, RC, dev, snapshot, nightly, etc.16- Extract segments, sub-version, suffix weight, group ID, or core version17- Use custom delimiters for non-standard formats (e.g. underscore-separated)1819## Decision Tree2021```22Raw version string → what do you need?23├─ Structured breakdown? → NewVersion() / version_parse / versions parse24├─ Yes/no validity? → IsValid() / version_validate / versions validate25├─ All type flags at once? → version_info / versions info26├─ Specific type check? → IsBeta(), IsStable(), IsRC() etc.27├─ Segments (major/minor/patch)? → Major()/Minor()/Patch()/Segments()28├─ Core version (no suffix)? → Core()29└─ Suffix weight for ordering? → SuffixWeight()30```3132## Task Patterns3334### Parse & inspect3536**Goal:** Break `"v1.2.3-rc1"` into prefix `"v"`, numbers `[1,2,3]`, suffix `"-rc1"`.3738| Layer | Approach |39|-------|----------|40| SDK | `v := versions.NewVersion("v1.2.3-rc1"); v.IsValid()` |41| CLI | `versions parse v1.2.3-rc1` |42| MCP | `{"tool": "version_parse", "arguments": {"version_string": "v1.2.3-rc1"}}` |4344### Validate4546**Goal:** Confirm `"1.2.3"` is valid, `"not-a-version"` is not.4748| Layer | Approach |49|-------|----------|50| SDK | `v.Validate() != nil` → invalid |51| CLI | `versions validate 1.2.3` (exit 0 = valid, exit 1 = invalid) |52| MCP | `{"tool": "version_validate", "arguments": {"version_string": "1.2.3"}}` |5354### Check type5556**Goal:** Determine if `"1.0.0-beta2"` is a beta prerelease.5758| Layer | Approach |59|-------|----------|60| SDK | `v.IsBeta()`, `v.IsPrerelease()`, `v.IsStable()`, `v.SubVersion()` |61| CLI | `versions check --beta 1.0.0-beta2` (exit 0 = match) |62| MCP | `{"tool": "version_info", ...}` returns all Is* flags |6364### Extract segments6566**Goal:** Get `[1, 2, 3]` from `"1.2.3"`.6768| Layer | Approach |69|-------|----------|70| SDK | `v.Major()`, `v.Minor()`, `v.Patch()`, `v.Segments()` |71| CLI | `versions segments 1.2.3` |72| MCP | `{"tool": "version_parse", ...}` → `segments` field |7374### Get core (strip suffix)7576**Goal:** `"v1.2.3-beta1"` → `"v1.2.3"`.7778| Layer | Approach |79|-------|----------|80| SDK | `v.Core().RawString()` |81| CLI | `versions core v1.2.3-beta1` |82| MCP | `{"tool": "version_core", "arguments": {"version_string": "v1.2.3-beta1"}}` |8384### Custom delimiters8586**Goal:** Parse `"curl-7_85_0"` with `-` and `_` delimiters.8788| Layer | Approach |89|-------|----------|90| SDK | `versions.NewVersionWithOption(s, versions.ParserOption{Delimiters: ".-_"})` |91| CLI | `versions parse --delimiters "_-" curl-7_85_0` |92| MCP | `{"tool": "version_parse", "arguments": {"version_string": "curl-7_85_0", "delimiters": ".-_"}}` |9394## API Reference9596### SDK — Constructor Functions9798```go99versions.NewVersion(versionStr string) *Version // never nil, check IsValid()100versions.NewVersionE(versionStr string) (*Version, error) // returns error for invalid101versions.MustParse(versionStr string) *Version // panics on invalid — test data only102versions.NewVersions(strings ...string) []*Version // batch parse103versions.NewVersionWithOption(s string, opt ParserOption) *Version // custom delimiters104versions.NewVersionStringParser(s string) *VersionStringParser // low-level parser105```106107### SDK — Version Struct Fields108109```go110type Version struct {111 Prefix string // e.g. "v"112 VersionNumbers []int // e.g. [1, 2, 3]113 Suffix string // e.g. "-rc1"114 Metadata string // semver build metadata (after +)115 PublicTime time.Time // optional release timestamp116 Raw string // original input string117}118```119120### SDK — Type Checks121122```go123v.IsValid() bool // has VersionNumbers124v.IsStable() bool // no suffix125v.IsPrerelease() bool // has any suffix126v.IsAlpha() bool // suffix contains "alpha"127v.IsBeta() bool // suffix contains "beta"128v.IsRC() bool // suffix contains "rc"129v.IsDev() bool // suffix contains "dev"130v.IsSnapshot() bool // suffix contains "snapshot"131v.IsNightly() bool // suffix contains "nightly"132v.IsMilestone() bool // suffix contains "milestone" or "m"133v.IsFinal() bool // suffix contains "final"134v.IsGA() bool // suffix contains "ga"135v.IsPre() bool // suffix contains "-pre"136v.IsRelease() bool // suffix contains "-release"137v.IsSP() bool // suffix contains "sp"138v.IsPost() bool // suffix contains "post"139```140141### SDK — Segment Accessors142143```go144v.Major() int // VersionNumbers[0] or 0145v.Minor() int // VersionNumbers[1] or 0146v.Patch() int // VersionNumbers[2] or 0147v.Segments() []int // all VersionNumbers148v.Segments64() []int64 // as int64149v.SubVersion() int // numeric from suffix (e.g. "beta2" → 2)150v.SuffixWeight() SuffixWeight // semantic ordering weight151```152153### SDK — Core, Clone, Serialization154155```go156v.Core() *Version // strip suffix157v.Clone() *Version // deep copy158v.BuildGroupID() string // e.g. "1.2.3"159v.WithPrefix(p string) *Version // immutable — returns new Version160v.WithSuffix(s string) *Version161v.WithMajor(n int) *Version162v.WithMinor(n int) *Version163v.WithPatch(n int) *Version164v.WithNumbers(ns []int) *Version165v.WithPublicTime(t time.Time) *Version166v.WithMetadata(m string) *Version167// JSON/Text/SQL serialization via MarshalText/UnmarshalText/MarshalJSON/UnmarshalJSON/Scan/Value168```169170### SDK — Parser Options171172```go173type ParserOption struct {174 Delimiters string // custom delimiter set, e.g. ".-_" (default: ".-")175}176```177178### CLI Commands179180```bash181versions parse <version> # structured JSON output182versions parse --delimiters "_-" <v> # custom delimiters183versions validate <version> # exit 0 = valid, 1 = invalid184versions info <version> # all Is* flags + segments185versions segments <version> # [major, minor, patch, ...]186versions sub-version <version> # numeric suffix index187versions suffix-weight <version> # semantic weight int188versions pure-prefix <version> # prefix without trailing delimiters189versions group-id <version> # e.g. "v1.2.3-beta" → "1.2.3"190versions core <version> # strip suffix191versions clone <version> # deep copy as JSON192versions check --<type> <version> # --alpha, --beta, --rc, --stable, etc.193versions check --prerelease <v> # exit 0 if prerelease194versions check --is-valid <v> # exit 0 if valid195```196197### MCP Tools198199| Tool | Arguments | Returns |200|------|-----------|---------|201| `version_parse` | `version_string`, `delimiters?` | prefix, numbers, suffix, metadata |202| `version_validate` | `version_string` | `{valid: bool, error: string?}` |203| `version_info` | `version_string` | all Is* flags, segments, suffix info |204| `version_core` | `version_string` | core version string |205206## Cross-References207208- [[version-check]] — boolean type checks, comparison predicates209- [[version-comparison]] — CompareTo, IsNewerThan, IsOlderThan210- [[version-sorting]] — sorting parsed versions211- [[version-constraints]] — constraint expression matching212- [[version-mutation]] — bumping, building, modifying versions213214## Important Notes215216- `NewVersion()` **never returns nil** — always check `IsValid()` for invalid input217- `IsValid()` checks for non-empty VersionNumbers; `Validate()` is stricter (rejects negative numbers)218- `IsPrerelease()` means "has any suffix"; `IsPre()` means "has explicit `-pre` suffix"219- `IsStable()` means "no suffix"; `IsRelease()` means "has explicit `-release` suffix"220- Semver build metadata (`+` part) is in `Metadata` field, **not** Suffix221- All `With*` methods return **new** Version objects — the original is never modified222- `MustParse` **panics** on invalid input — use only for hardcoded/test data