Nushell can be extended with plugins to have more functionality or types that are not part of standard Nushell.
If you want to work with string values that are actually semantic version values (https://semver.org) you can use the Nushell SemVer plugin.
The plugin must be added to Nushell by using the command plugin add <location of plugin>.
You can check with plugin list command if the plugin is available.
This command also shows commands that the plugin adds to Nushell.
The command into semver can be used to convert string values into a semver type.
The semver type has 5 properties: major, minor, patch, pre and build.
You can use dot notation to get the values for these properties or use the get command.
In the following command a string value is transformed to a semver type: let v = '4.0.2' | into semver.
And with $v.major you can extract the major part of the semver value and it returns 4.
Records with the keys major, minor, patch, pre and build can be transformed to a semver string with the semver from-record command.
And to transform a semver type to a record you can use the command semver into-record.
In the following example different string values are transformed and from a semver type or record:
use std/assert
# After installing the nu_plugin_semver plugin and
# adding it to the plugin registry you can use
# the plugin for Nushell code.
plugin use semver
# With `into semver` you can transform a string
# into a semver value with the fields
# major, minor, patch, pre and build.
let version = '4.0.2' | into semver
assert equal $version.major 4
assert equal $version.minor 0
assert equal $version.patch 2
assert equal ($version | get patch) 2
# Type is semver and no longer a string type.
assert equal ($version | describe) semver
# A full version with all parts.
# The pre (release) part is after the patch and - up until the +.
# The build part is everything after the +.
let full_version = '4.0.2-rc.1+build-20260205' | into semver
assert equal $full_version.major 4
assert equal $full_version.minor 0
assert equal $full_version.patch 2
assert equal $full_version.pre 'rc.1'
assert equal $full_version.build 'build-20260205'
# A version with major, minor, patch and build part.
let build_version = '4.0.2+build-20260205' | into semver
assert equal $build_version.build 'build-20260205'
assert equal $build_version.major 4
assert equal $build_version.minor 0
assert equal $build_version.patch 2
# Using `semver to-record` to transform a semver value or string
# to a record with the keys major, minor, patch, pre and build.
assert equal ($version | semver to-record) {
major: 4 minor: 0 patch: 2 pre: '' build: ''
}
assert equal ('1.2.3' | semver to-record) {
major: 1 minor: 2 patch: 3 pre: '' build: ''
}
assert equal ('4.0.2-rc.1+build-20260205' | semver to-record) {
major: 4 minor: 0 patch: 2 pre: 'rc.1' build: 'build-20260205'
}
# Using `semver from-record` to transform a record with keys
# major, minor, patch, pre and build to a semver value.
(assert equal
({major: 4 minor: 0 patch: 2 pre: '' build: ''} | semver from-record)
'4.0.2')
(assert equal
({major: 4 minor: 0 patch: 2 pre: 'rc.1' build: 'build-20260205'} | semver from-record)
'4.0.2-rc.1+build-20260205')
# A list of string value can be transformed to a list of semver values.
(assert equal (['4.0.2-rc.1+build-20260205' '1.2.3'] | into semver)
[('4.0.2-rc.1+build-20260205' | into semver) ('1.2.3' | into semver)])
Written with Nushell 0.110.0.